
<!DOCTYPE html>
<html>

	<head>
	<title>CAIJL's Blog</title>
	<meta charset="utf-8"/>
	<link rel="stylesheet" href="/font-awesome-4.7.0/css/font-awesome.min.css">

	<link rel="stylesheet" href="/bootstrap-3.3.7/css/bootstrap.min.css">
	<script src="/jquery-2.1.1/jquery.min.js"></script>
	<script src="/bootstrap-3.3.7/js/bootstrap.min.js"></script>

	<link rel="stylesheet" href="/css/bootstrap-slider.min.css">
	<script src="/js/bootstrap-slider.min.js"></script>
	
	<link rel="stylesheet" href="/css/main.css">
	<script src="/js/main.js"></script>
	
	<link rel="stylesheet" href="/css/cursor.css">
	<script src="/js/cursor.js"></script>
	
	
	<link rel="stylesheet" href="/css/code.css">
	<!--link rel="stylesheet" href="/css/markdown.css"-->
	
	<link rel="stylesheet" href="/css/head.css">

	<script src="/js/window.js"></script>
	<script src='//unpkg.com/valine/dist/Valine.min.js'></script>
	</head>
<body>

	<link rel="stylesheet" href="/katex/katex.min.css" crossorigin="anonymous">
	<script src="/katex/katex.min.js" crossorigin="anonymous"></script>
	<script src="/katex/contrib/mathtex-script-type.min.js" defer></script>
	<script defer src="/katex/contrib/auto-render.min.js"crossorigin="anonymous"
	    onload="renderMathInElement(document.body);"></script>
	<script>document.addEventListener("DOMContentLoaded", function() {renderMathInElement(document.body,{"delimiters":[{left: "$", right: "$", display: false}]});});</script>
	<nav class="navbar navbar-default header card" role="navigation" style="width: 100%;">
	<div class="container-fluid"> 
	<div class="navbar-header">
		<button type="button" class="navbar-toggle" data-toggle="collapse"
				data-target="#example-navbar-collapse">
			<span class="icon-bar"></span>
			<span class="icon-bar"></span>
			<span class="icon-bar"></span>
		</button>
		<a class="navbar-brand" href="/"><span><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi mathvariant="bold">C</mi><mi mathvariant="bold">A</mi><mi mathvariant="bold">I</mi><mi mathvariant="bold">J</mi><msup><mi mathvariant="bold">L</mi><mo mathvariant="bold" lspace="0em" rspace="0em">′</mo></msup><mi mathvariant="bold">s</mi><mi mathvariant="bold">B</mi><mi mathvariant="bold">L</mi><mi mathvariant="bold">O</mi><mi mathvariant="bold">G</mi></mrow><annotation encoding="application/x-tex">\mathbf{CAIJL'sBLOG}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height: 0.751892em; vertical-align: 0em;"></span><span class="mord"><span class="mord mathbf">C</span><span class="mord mathbf">A</span><span class="mord mathbf">I</span><span class="mord mathbf">J</span><span class="mord"><span class="mord mathbf">L</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height: 0.751892em;"><span class="" style="top: -3.063em; margin-right: 0.05em;"><span class="pstrut" style="height: 2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathbf mtight">′</span></span></span></span></span></span></span></span></span><span class="mord mathbf">s</span><span class="mord mathbf">B</span><span class="mord mathbf">L</span><span class="mord mathbf">O</span><span class="mord mathbf">G</span></span></span></span></span></span></a>
	</div>
	<div class="collapse navbar-collapse" id="example-navbar-collapse">
		<ul class="nav navbar-nav" style="background-color:#fcfcfc">
			<li><a href="/"><i class="menu-item-icon fa fa-fw fa-home"></i>首页</a></li>
			<li><a href="/archives/"><i class="menu-item-icon fa fa-fw fa-archive"></i>文章</a></li>
			<li><a href="/tags/"><i class="menu-item-icon fa fa-fw fa-tags"></i>标签</a></li>
			<li><a href="/settings/"><i class="menu-item-icon fa fa-fw fa-cogs"></i>设置</a></li>
			<!--li><a href="/about/"><i class="menu-item-icon fa fa-fw fa-user"></i>关于</a></li-->
			<li><a href="/games/"><i class="menu-item-icon fa fa-fw fa-gamepad"></i>其它</a></li>
			

		</ul>
	</div>
	</div>
	</nav>
<div class="container">
	<div style="height: 60px;"></div>
	<div class="row" >
		<div class="col-md-8">
			<div class="panel panel-default card" style="padding: 10px;position: relative;">
                <img src='/img/bg/40.png' style="width:100%"/>
				<span style="position: absolute;left:15px;bottom:15px;width:90%;"><font class="view-text" style="color:#fcfcfc;font-size:25px">[营业日志]萌新的多项式入门</font><br><a href="/tags/2020/" class="tag"><span  style="background-color: rgb(52, 152, 219);">2020</span></a></span>
			</div>
			<div class="panel panel-default card" style="padding: 10px;" id="main">
                <p>
<script type="math/tex; mode=display">\text{逐渐消逝的未来}</script>
</p>
<!--more-->
<h2 id="_1">多项式工业入门</h2>
<h3 id="_2">一些定义</h3>
<div class="highlight"><pre><span></span><code><span class="linenos" data-linenos=" 1 "></span><span class="k">template</span><span class="o">&lt;</span><span class="k">const</span> <span class="kt">int</span> <span class="n">mod</span><span class="o">&gt;</span>
<span class="linenos" data-linenos=" 2 "></span><span class="k">struct</span> <span class="nc">modint</span><span class="p">{</span>
<span class="linenos" data-linenos=" 3 "></span>    <span class="kt">int</span> <span class="n">x</span><span class="p">;</span>
<span class="linenos" data-linenos=" 4 "></span>    <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span><span class="p">(</span><span class="kt">int</span> <span class="n">o</span><span class="o">=</span><span class="mi">0</span><span class="p">){</span><span class="n">x</span><span class="o">=</span><span class="n">o</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 5 "></span>    <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">=</span> <span class="p">(</span><span class="kt">int</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">x</span><span class="o">=</span><span class="n">o</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 6 "></span>    <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">+=</span><span class="p">(</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">x</span><span class="o">=</span><span class="n">x</span><span class="o">+</span><span class="n">o</span><span class="p">.</span><span class="n">x</span><span class="o">&gt;=</span><span class="n">mod</span><span class="o">?</span><span class="n">x</span><span class="o">+</span><span class="n">o</span><span class="p">.</span><span class="n">x</span><span class="o">-</span><span class="nl">mod</span><span class="p">:</span><span class="n">x</span><span class="o">+</span><span class="n">o</span><span class="p">.</span><span class="n">x</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 7 "></span>    <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">-=</span><span class="p">(</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">x</span><span class="o">=</span><span class="n">x</span><span class="o">-</span><span class="n">o</span><span class="p">.</span><span class="n">x</span><span class="o">&lt;</span><span class="mi">0</span><span class="o">?</span><span class="n">x</span><span class="o">-</span><span class="n">o</span><span class="p">.</span><span class="n">x</span><span class="o">+</span><span class="nl">mod</span><span class="p">:</span><span class="n">x</span><span class="o">-</span><span class="n">o</span><span class="p">.</span><span class="n">x</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 8 "></span>    <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">*=</span><span class="p">(</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">x</span><span class="o">=</span><span class="mf">1l</span><span class="n">l</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">o</span><span class="p">.</span><span class="n">x</span><span class="o">%</span><span class="n">mod</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 9 "></span>    <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">^=</span><span class="p">(</span><span class="kt">int</span> <span class="n">b</span><span class="p">){</span>
<span class="linenos" data-linenos="10 "></span>        <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">a</span><span class="o">=*</span><span class="k">this</span><span class="p">,</span><span class="n">c</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span>
<span class="linenos" data-linenos="11 "></span>        <span class="k">for</span><span class="p">(;</span><span class="n">b</span><span class="p">;</span><span class="n">b</span><span class="o">&gt;&gt;=</span><span class="mi">1</span><span class="p">,</span><span class="n">a</span><span class="o">*=</span><span class="n">a</span><span class="p">)</span><span class="k">if</span><span class="p">(</span><span class="n">b</span><span class="o">&amp;</span><span class="mi">1</span><span class="p">)</span><span class="n">c</span><span class="o">*=</span><span class="n">a</span><span class="p">;</span>
<span class="linenos" data-linenos="12 "></span>        <span class="k">return</span> <span class="n">x</span><span class="o">=</span><span class="n">c</span><span class="p">.</span><span class="n">x</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;</span>
<span class="linenos" data-linenos="13 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="14 "></span>    <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">/=</span><span class="p">(</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="o">*</span><span class="k">this</span> <span class="o">*=</span><span class="n">o</span><span class="o">^=</span><span class="n">mod</span><span class="mi">-2</span><span class="p">;}</span>
<span class="linenos" data-linenos="15 "></span>    <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">+=</span><span class="p">(</span><span class="kt">int</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">x</span><span class="o">=</span><span class="n">x</span><span class="o">+</span><span class="n">o</span><span class="o">&gt;=</span><span class="n">mod</span><span class="o">?</span><span class="n">x</span><span class="o">+</span><span class="n">o</span><span class="o">-</span><span class="nl">mod</span><span class="p">:</span><span class="n">x</span><span class="o">+</span><span class="n">o</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos="16 "></span>    <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">-=</span><span class="p">(</span><span class="kt">int</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">x</span><span class="o">=</span><span class="n">x</span><span class="o">-</span><span class="n">o</span><span class="o">&lt;</span><span class="mi">0</span><span class="o">?</span><span class="n">x</span><span class="o">-</span><span class="n">o</span><span class="o">+</span><span class="nl">mod</span><span class="p">:</span><span class="n">x</span><span class="o">-</span><span class="n">o</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos="17 "></span>    <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">*=</span><span class="p">(</span><span class="kt">int</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">x</span><span class="o">=</span><span class="mf">1l</span><span class="n">l</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">o</span><span class="o">%</span><span class="n">mod</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos="18 "></span>    <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">/=</span><span class="p">(</span><span class="kt">int</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="o">*</span><span class="k">this</span> <span class="o">*=</span> <span class="p">((</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span><span class="p">(</span><span class="n">o</span><span class="p">))</span><span class="o">^=</span><span class="n">mod</span><span class="mi">-2</span><span class="p">);}</span>
<span class="linenos" data-linenos="19 "></span>    <span class="k">template</span><span class="o">&lt;</span><span class="k">class</span> <span class="nc">I</span><span class="o">&gt;</span><span class="k">friend</span> <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="k">operator</span> <span class="o">+</span><span class="p">(</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">a</span><span class="p">,</span><span class="n">I</span> <span class="n">b</span><span class="p">){</span><span class="k">return</span> <span class="n">a</span><span class="o">+=</span><span class="n">b</span><span class="p">;}</span>
<span class="linenos" data-linenos="20 "></span>    <span class="k">template</span><span class="o">&lt;</span><span class="k">class</span> <span class="nc">I</span><span class="o">&gt;</span><span class="k">friend</span> <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="k">operator</span> <span class="o">-</span><span class="p">(</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">a</span><span class="p">,</span><span class="n">I</span> <span class="n">b</span><span class="p">){</span><span class="k">return</span> <span class="n">a</span><span class="o">-=</span><span class="n">b</span><span class="p">;}</span>
<span class="linenos" data-linenos="21 "></span>    <span class="k">template</span><span class="o">&lt;</span><span class="k">class</span> <span class="nc">I</span><span class="o">&gt;</span><span class="k">friend</span> <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="k">operator</span> <span class="o">*</span><span class="p">(</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">a</span><span class="p">,</span><span class="n">I</span> <span class="n">b</span><span class="p">){</span><span class="k">return</span> <span class="n">a</span><span class="o">*=</span><span class="n">b</span><span class="p">;}</span>
<span class="linenos" data-linenos="22 "></span>    <span class="k">template</span><span class="o">&lt;</span><span class="k">class</span> <span class="nc">I</span><span class="o">&gt;</span><span class="k">friend</span> <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="k">operator</span> <span class="o">/</span><span class="p">(</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">a</span><span class="p">,</span><span class="n">I</span> <span class="n">b</span><span class="p">){</span><span class="k">return</span> <span class="n">a</span><span class="o">/=</span><span class="n">b</span><span class="p">;}</span>
<span class="linenos" data-linenos="23 "></span>    <span class="k">friend</span> <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="k">operator</span> <span class="o">^</span><span class="p">(</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">a</span><span class="p">,</span><span class="kt">int</span> <span class="n">b</span><span class="p">){</span><span class="k">return</span> <span class="n">a</span><span class="o">^=</span><span class="n">b</span><span class="p">;}</span>
<span class="linenos" data-linenos="24 "></span>    <span class="k">friend</span> <span class="kt">bool</span> <span class="k">operator</span> <span class="o">==</span><span class="p">(</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">a</span><span class="p">,</span><span class="kt">int</span> <span class="n">b</span><span class="p">){</span><span class="k">return</span> <span class="n">a</span><span class="p">.</span><span class="n">x</span><span class="o">==</span><span class="n">b</span><span class="p">;}</span>
<span class="linenos" data-linenos="25 "></span>    <span class="k">friend</span> <span class="kt">bool</span> <span class="k">operator</span> <span class="o">!=</span><span class="p">(</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">a</span><span class="p">,</span><span class="kt">int</span> <span class="n">b</span><span class="p">){</span><span class="k">return</span> <span class="n">a</span><span class="p">.</span><span class="n">x</span><span class="o">!=</span><span class="n">b</span><span class="p">;}</span>
<span class="linenos" data-linenos="26 "></span>    <span class="kt">bool</span> <span class="k">operator</span> <span class="o">!</span> <span class="p">()</span> <span class="p">{</span><span class="k">return</span> <span class="o">!</span><span class="n">x</span><span class="p">;}</span>
<span class="linenos" data-linenos="27 "></span>    <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="k">operator</span> <span class="o">-</span> <span class="p">()</span> <span class="p">{</span><span class="k">return</span> <span class="n">x</span><span class="o">?</span><span class="n">mod</span><span class="o">-</span><span class="nl">x</span><span class="p">:</span><span class="mi">0</span><span class="p">;}</span>
<span class="linenos" data-linenos="28 "></span>    <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="o">&amp;</span><span class="k">operator</span><span class="o">++</span><span class="p">(</span><span class="kt">int</span><span class="p">){</span><span class="k">return</span> <span class="o">*</span><span class="k">this</span><span class="o">+=</span><span class="mi">1</span><span class="p">;}</span>
<span class="linenos" data-linenos="29 "></span><span class="p">};</span>
<span class="linenos" data-linenos="30 "></span><span class="k">const</span> <span class="kt">int</span> <span class="n">N</span><span class="o">=</span><span class="mf">4e6</span><span class="o">+</span><span class="mi">5</span><span class="p">;</span>
<span class="linenos" data-linenos="31 "></span>
<span class="linenos" data-linenos="32 "></span><span class="k">const</span> <span class="kt">int</span> <span class="n">mod</span><span class="o">=</span><span class="mi">998244353</span><span class="p">;</span>
<span class="linenos" data-linenos="33 "></span><span class="k">const</span> <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">GG</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span><span class="n">Ginv</span><span class="o">=</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="mi">3</span><span class="p">,</span><span class="n">I</span><span class="o">=</span><span class="mi">86583718</span><span class="p">;</span>
<span class="linenos" data-linenos="34 "></span><span class="k">struct</span> <span class="nc">poly</span><span class="p">{</span>
<span class="linenos" data-linenos="35 "></span>    <span class="n">vector</span><span class="o">&lt;</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;&gt;</span><span class="n">a</span><span class="p">;</span>
<span class="linenos" data-linenos="36 "></span>    <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;&amp;</span><span class="k">operator</span><span class="p">[](</span><span class="kt">int</span> <span class="n">i</span><span class="p">){</span><span class="k">return</span> <span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">];}</span>
<span class="linenos" data-linenos="37 "></span>    <span class="kt">int</span> <span class="n">size</span><span class="p">(){</span><span class="k">return</span> <span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">();}</span>
<span class="linenos" data-linenos="38 "></span>    <span class="kt">void</span> <span class="n">resize</span><span class="p">(</span><span class="kt">int</span> <span class="n">n</span><span class="p">){</span><span class="n">a</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);}</span>
<span class="linenos" data-linenos="39 "></span>    <span class="kt">void</span> <span class="n">reverse</span><span class="p">(){</span><span class="n">std</span><span class="o">::</span><span class="n">reverse</span><span class="p">(</span><span class="n">a</span><span class="p">.</span><span class="n">begin</span><span class="p">(),</span><span class="n">a</span><span class="p">.</span><span class="n">end</span><span class="p">());}</span>
<span class="linenos" data-linenos="40 "></span><span class="p">};</span>
<span class="linenos" data-linenos="41 "></span><span class="kt">int</span> <span class="n">rev</span><span class="p">[</span><span class="n">N</span><span class="p">];</span>
<span class="linenos" data-linenos="42 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="n">one</span><span class="p">(){</span><span class="n">poly</span> <span class="n">a</span><span class="p">;</span><span class="n">a</span><span class="p">.</span><span class="n">a</span><span class="p">.</span><span class="n">push_back</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span><span class="k">return</span> <span class="n">a</span><span class="p">;}</span>
</code></pre></div>
<h3 id="_3">基础快速变幻</h3>
<div class="highlight"><pre><span></span><code><span class="linenos" data-linenos=" 1 "></span><span class="kr">inline</span> <span class="kt">int</span> <span class="n">ext</span><span class="p">(</span><span class="kt">int</span> <span class="n">n</span><span class="p">){</span><span class="kt">int</span> <span class="n">k</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="k">while</span><span class="p">((</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="n">k</span><span class="p">)</span><span class="o">&lt;</span><span class="n">n</span><span class="p">)</span><span class="n">k</span><span class="o">++</span><span class="p">;</span><span class="k">return</span> <span class="n">k</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 2 "></span><span class="kr">inline</span> <span class="kt">void</span> <span class="n">init</span><span class="p">(</span><span class="kt">int</span> <span class="n">k</span><span class="p">){</span><span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="n">k</span><span class="p">;</span><span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">rev</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="p">(</span><span class="n">rev</span><span class="p">[</span><span class="n">i</span><span class="o">&gt;&gt;</span><span class="mi">1</span><span class="p">]</span><span class="o">&gt;&gt;</span><span class="mi">1</span><span class="p">)</span><span class="o">|</span><span class="p">((</span><span class="n">i</span><span class="o">&amp;</span><span class="mi">1</span><span class="p">)</span><span class="o">&lt;&lt;</span><span class="p">(</span><span class="n">k</span><span class="mi">-1</span><span class="p">));}</span>
<span class="linenos" data-linenos=" 3 "></span><span class="kr">inline</span> <span class="kt">void</span> <span class="n">ntt</span><span class="p">(</span><span class="n">poly</span><span class="o">&amp;</span><span class="n">a</span><span class="p">,</span><span class="kt">int</span> <span class="n">k</span><span class="p">,</span><span class="kt">int</span> <span class="n">typ</span><span class="p">){</span>
<span class="linenos" data-linenos=" 4 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="n">k</span><span class="p">;</span>
<span class="linenos" data-linenos=" 5 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="k">if</span><span class="p">(</span><span class="n">i</span><span class="o">&lt;</span><span class="n">rev</span><span class="p">[</span><span class="n">i</span><span class="p">])</span><span class="n">swap</span><span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">],</span><span class="n">a</span><span class="p">[</span><span class="n">rev</span><span class="p">[</span><span class="n">i</span><span class="p">]]);</span>
<span class="linenos" data-linenos=" 6 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">mid</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span><span class="n">mid</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">mid</span><span class="o">&lt;&lt;=</span><span class="mi">1</span><span class="p">){</span>
<span class="linenos" data-linenos=" 7 "></span>        <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">wn</span><span class="o">=</span><span class="p">(</span><span class="n">typ</span><span class="o">&gt;</span><span class="mi">0</span><span class="o">?</span><span class="nl">GG</span><span class="p">:</span><span class="n">Ginv</span><span class="p">)</span><span class="o">^</span><span class="p">((</span><span class="n">mod</span><span class="mi">-1</span><span class="p">)</span><span class="o">/</span><span class="p">(</span><span class="n">mid</span><span class="o">&lt;&lt;</span><span class="mi">1</span><span class="p">));</span>
<span class="linenos" data-linenos=" 8 "></span>        <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">r</span><span class="o">=</span><span class="n">mid</span><span class="o">&lt;&lt;</span><span class="mi">1</span><span class="p">,</span><span class="n">j</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">j</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">j</span><span class="o">+=</span><span class="n">r</span><span class="p">){</span>
<span class="linenos" data-linenos=" 9 "></span>            <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">w</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span>
<span class="linenos" data-linenos="10 "></span>            <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">k</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">k</span><span class="o">&lt;</span><span class="n">mid</span><span class="p">;</span><span class="n">k</span><span class="o">++</span><span class="p">,</span><span class="n">w</span><span class="o">=</span><span class="n">w</span><span class="o">*</span><span class="n">wn</span><span class="p">){</span>
<span class="linenos" data-linenos="11 "></span>                <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">x</span><span class="o">=</span><span class="n">a</span><span class="p">[</span><span class="n">j</span><span class="o">+</span><span class="n">k</span><span class="p">],</span><span class="n">y</span><span class="o">=</span><span class="n">w</span><span class="o">*</span><span class="n">a</span><span class="p">[</span><span class="n">j</span><span class="o">+</span><span class="n">k</span><span class="o">+</span><span class="n">mid</span><span class="p">];</span>
<span class="linenos" data-linenos="12 "></span>                <span class="n">a</span><span class="p">[</span><span class="n">j</span><span class="o">+</span><span class="n">k</span><span class="p">]</span><span class="o">=</span><span class="n">x</span><span class="o">+</span><span class="n">y</span><span class="p">,</span><span class="n">a</span><span class="p">[</span><span class="n">j</span><span class="o">+</span><span class="n">k</span><span class="o">+</span><span class="n">mid</span><span class="p">]</span><span class="o">=</span><span class="n">x</span><span class="o">-</span><span class="n">y</span><span class="p">;</span>
<span class="linenos" data-linenos="13 "></span>            <span class="p">}</span>
<span class="linenos" data-linenos="14 "></span>        <span class="p">}</span>
<span class="linenos" data-linenos="15 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="16 "></span>    <span class="k">if</span><span class="p">(</span><span class="n">typ</span><span class="o">&lt;</span><span class="mi">0</span><span class="p">){</span>
<span class="linenos" data-linenos="17 "></span>        <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">inv</span><span class="o">=</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="n">n</span><span class="p">;</span>
<span class="linenos" data-linenos="18 "></span>        <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">*=</span><span class="n">inv</span><span class="p">;</span>
<span class="linenos" data-linenos="19 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="20 "></span><span class="p">}</span>
</code></pre></div>
<h3 id="_4">多项式加、减、乘</h3>
<div class="highlight"><pre><span></span><code><span class="linenos" data-linenos=" 1 "></span><span class="n">poly</span> <span class="k">operator</span> <span class="o">+</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">,</span><span class="n">poly</span> <span class="n">b</span><span class="p">){</span>
<span class="linenos" data-linenos=" 2 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="n">max</span><span class="p">(</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">(),</span><span class="n">b</span><span class="p">.</span><span class="n">size</span><span class="p">());</span><span class="n">a</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">),</span><span class="n">b</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="linenos" data-linenos=" 3 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">+=</span><span class="n">b</span><span class="p">[</span><span class="n">i</span><span class="p">];</span><span class="k">return</span> <span class="n">a</span><span class="p">;</span>
<span class="linenos" data-linenos=" 4 "></span><span class="p">}</span>
<span class="linenos" data-linenos=" 5 "></span><span class="n">poly</span> <span class="k">operator</span> <span class="o">-</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">,</span><span class="n">poly</span> <span class="n">b</span><span class="p">){</span>
<span class="linenos" data-linenos=" 6 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="n">max</span><span class="p">(</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">(),</span><span class="n">b</span><span class="p">.</span><span class="n">size</span><span class="p">());</span><span class="n">a</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">),</span><span class="n">b</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="linenos" data-linenos=" 7 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-=</span><span class="n">b</span><span class="p">[</span><span class="n">i</span><span class="p">];</span><span class="k">return</span> <span class="n">a</span><span class="p">;</span>
<span class="linenos" data-linenos=" 8 "></span><span class="p">}</span>
<span class="linenos" data-linenos=" 9 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="k">operator</span><span class="o">*</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">,</span><span class="n">poly</span> <span class="n">b</span><span class="p">){</span>
<span class="linenos" data-linenos="10 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">()</span><span class="o">+</span><span class="n">b</span><span class="p">.</span><span class="n">size</span><span class="p">()</span><span class="mi">-1</span><span class="p">,</span><span class="n">k</span><span class="o">=</span><span class="n">ext</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="linenos" data-linenos="11 "></span>    <span class="n">a</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="n">k</span><span class="p">),</span><span class="n">b</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="n">k</span><span class="p">),</span><span class="n">init</span><span class="p">(</span><span class="n">k</span><span class="p">);</span>
<span class="linenos" data-linenos="12 "></span>    <span class="n">ntt</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">k</span><span class="p">,</span><span class="mi">1</span><span class="p">);</span><span class="n">ntt</span><span class="p">(</span><span class="n">b</span><span class="p">,</span><span class="n">k</span><span class="p">,</span><span class="mi">1</span><span class="p">);</span><span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="p">(</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="n">k</span><span class="p">);</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">*=</span><span class="n">b</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
<span class="linenos" data-linenos="13 "></span>    <span class="n">ntt</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">k</span><span class="p">,</span><span class="mi">-1</span><span class="p">),</span><span class="n">a</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span><span class="k">return</span> <span class="n">a</span><span class="p">;</span>
<span class="linenos" data-linenos="14 "></span><span class="p">}</span>
<span class="linenos" data-linenos="15 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="k">operator</span><span class="o">*</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">,</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">b</span><span class="p">){</span><span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">();</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">*=</span><span class="n">b</span><span class="p">;</span><span class="k">return</span> <span class="n">a</span><span class="p">;</span> <span class="p">}</span>
<span class="linenos" data-linenos="16 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="k">operator</span><span class="o">/</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">,</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">b</span><span class="p">){</span><span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">();</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">/=</span><span class="n">b</span><span class="p">;</span><span class="k">return</span> <span class="n">a</span><span class="p">;</span> <span class="p">}</span>
<span class="linenos" data-linenos="17 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="k">operator</span><span class="o">-</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">){</span><span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">();</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=-</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">];</span><span class="k">return</span> <span class="n">a</span><span class="p">;</span> <span class="p">}</span>
</code></pre></div>
<h3 id="_5">多项式求逆</h3>
<p>如果多项式<script type="math/tex">F</script>只有一项，那么显然<script type="math/tex">G_0</script>就是<script type="math/tex">F_0</script>的逆元。</p>
<p>若有<script type="math/tex">n</script>项，递归求解。</p>
<p>假如我们已知<script type="math/tex">F(x)H(x) \equiv 1 \pmod{x^{\lceil \frac{n}{2} \rceil}}</script>
</p>
<p>又显然<script type="math/tex">F(x)G(x) \equiv 1 \pmod{x^{\lceil \frac{n}{2} \rceil}}</script>
</p>
<p>那么<script type="math/tex">F(x)(G(x)-H(x))\equiv 0 \pmod{x^{\lceil \frac{n}{2} \rceil}}</script>
</p>
<p>即<script type="math/tex">G(x)-H(x)\equiv 0 \pmod{x^{\lceil \frac{n}{2} \rceil}}</script>
</p>
<p>两边同时平方。</p>
<p>
<script type="math/tex">G(x)^2+H(x)^2-2G(x)H(x) \equiv 0 \pmod{x^n}</script>
</p>
<p>两边同时乘<script type="math/tex">F(x)</script>，再由<script type="math/tex">F(x)G(x) \equiv 1 \pmod{x^n}</script>，得出：</p>
<p>
<script type="math/tex">G(x) \equiv 2H(x)-F(x)H(x)^2 \pmod{x^n}</script>
<div class="highlight"><pre><span></span><code><span class="linenos" data-linenos=" 1 "></span><span class="n">poly</span> <span class="nf">inv</span><span class="p">(</span><span class="n">poly</span> <span class="n">F</span><span class="p">,</span><span class="kt">int</span> <span class="n">k</span><span class="p">){</span>
<span class="linenos" data-linenos=" 2 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="n">k</span><span class="p">;</span><span class="n">F</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="linenos" data-linenos=" 3 "></span>    <span class="k">if</span><span class="p">(</span><span class="n">n</span><span class="o">==</span><span class="mi">1</span><span class="p">){</span><span class="n">F</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">=</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="n">F</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span><span class="k">return</span> <span class="n">F</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 4 "></span>    <span class="n">poly</span> <span class="n">G</span><span class="p">,</span><span class="n">H</span><span class="o">=</span><span class="n">inv</span><span class="p">(</span><span class="n">F</span><span class="p">,</span><span class="n">k</span><span class="mi">-1</span><span class="p">);</span>
<span class="linenos" data-linenos=" 5 "></span>    <span class="n">G</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">),</span><span class="n">H</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="o">&lt;&lt;</span><span class="mi">1</span><span class="p">),</span><span class="n">F</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="o">&lt;&lt;</span><span class="mi">1</span><span class="p">);</span>
<span class="linenos" data-linenos=" 6 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="o">/</span><span class="mi">2</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">G</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="n">H</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">*</span><span class="mi">2</span><span class="p">;</span>
<span class="linenos" data-linenos=" 7 "></span>    <span class="n">init</span><span class="p">(</span><span class="n">k</span><span class="o">+</span><span class="mi">1</span><span class="p">),</span><span class="n">ntt</span><span class="p">(</span><span class="n">H</span><span class="p">,</span><span class="n">k</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">),</span><span class="n">ntt</span><span class="p">(</span><span class="n">F</span><span class="p">,</span><span class="n">k</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">);</span>
<span class="linenos" data-linenos=" 8 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="p">(</span><span class="n">n</span><span class="o">&lt;&lt;</span><span class="mi">1</span><span class="p">);</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">H</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="n">H</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">*</span><span class="n">H</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">*</span><span class="n">F</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
<span class="linenos" data-linenos=" 9 "></span>    <span class="n">ntt</span><span class="p">(</span><span class="n">H</span><span class="p">,</span><span class="n">k</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span><span class="mi">-1</span><span class="p">),</span><span class="n">H</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="linenos" data-linenos="10 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">G</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-=</span><span class="n">H</span><span class="p">[</span><span class="n">i</span><span class="p">];</span><span class="k">return</span> <span class="n">G</span><span class="p">;</span>
<span class="linenos" data-linenos="11 "></span><span class="p">}</span>
<span class="linenos" data-linenos="12 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="n">inv</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">){</span>
<span class="linenos" data-linenos="13 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">();</span>
<span class="linenos" data-linenos="14 "></span>    <span class="n">a</span><span class="o">=</span><span class="n">inv</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">ext</span><span class="p">(</span><span class="n">n</span><span class="p">)),</span><span class="n">a</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span><span class="k">return</span> <span class="n">a</span><span class="p">;;</span>
<span class="linenos" data-linenos="15 "></span><span class="p">}</span>
</code></pre></div></p>
<h3 id="_6">求导数、原函数</h3>
<p>
<script type="math/tex">(x^a)^\prime=ax^{a-1}</script>
</p>
<p>
<script type="math/tex">\int x^adx=\frac{1}{a+1}x^{a+1}</script>
</p>
<div class="highlight"><pre><span></span><code><span class="linenos" data-linenos=" 1 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="n">deriv</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">){</span><span class="c1">//求导 </span>
<span class="linenos" data-linenos=" 2 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">()</span><span class="mi">-1</span><span class="p">;</span>
<span class="linenos" data-linenos=" 3 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span><span class="o">*</span><span class="p">(</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">);</span>
<span class="linenos" data-linenos=" 4 "></span>    <span class="n">a</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span><span class="k">return</span> <span class="n">a</span><span class="p">;</span>
<span class="linenos" data-linenos=" 5 "></span><span class="p">}</span>
<span class="linenos" data-linenos=" 6 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="n">inter</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">){</span><span class="c1">//求原 </span>
<span class="linenos" data-linenos=" 7 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">()</span><span class="o">+</span><span class="mi">1</span><span class="p">;</span><span class="n">a</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="linenos" data-linenos=" 8 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">&gt;=</span><span class="mi">1</span><span class="p">;</span><span class="n">i</span><span class="o">--</span><span class="p">)</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="mi">-1</span><span class="p">]</span><span class="o">/</span><span class="n">i</span><span class="p">;</span>
<span class="linenos" data-linenos=" 9 "></span>    <span class="n">a</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="k">return</span> <span class="n">a</span><span class="p">;</span>
<span class="linenos" data-linenos="10 "></span><span class="p">}</span>
</code></pre></div>
<h3 id="ln">多项式<script type="math/tex">\ln</script>
</h3>
<p>
<script type="math/tex">G(x)=F(A(x)),F(x)=ln(x)</script>
</p>
<p>两边同时求导，得到：</p>
<p>
<script type="math/tex">G(x)^\prime=F^\prime(A(x))A^\prime(x)=\frac{A^\prime(x)}{A(x)}</script>
</p>
<p>在求一遍逆即可。</p>
<div class="highlight"><pre><span></span><code><span class="linenos" data-linenos="1 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="n">ln</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">){</span>
<span class="linenos" data-linenos="2 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">();</span>
<span class="linenos" data-linenos="3 "></span>    <span class="n">a</span><span class="o">=</span><span class="n">inter</span><span class="p">(</span><span class="n">deriv</span><span class="p">(</span><span class="n">a</span><span class="p">)</span><span class="o">*</span><span class="n">inv</span><span class="p">(</span><span class="n">a</span><span class="p">));</span>
<span class="linenos" data-linenos="4 "></span>    <span class="n">a</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span><span class="k">return</span> <span class="n">a</span><span class="p">;</span>
<span class="linenos" data-linenos="5 "></span><span class="p">}</span>
</code></pre></div>
<h3 id="exp">多项式<script type="math/tex">\exp</script>
</h3>
<p>计算<script type="math/tex">F(x)=e^{A(x)}</script>
</p>
<p>变形得<script type="math/tex">\ln F(x)-A(x)=0</script>
</p>
<p>设<script type="math/tex">G(F(x))=lnF(x)-A(x)</script>，那么就是要求这一个函数的零点。</p>
<p>那么我们把<script type="math/tex">F(x)</script>看做变量，<script type="math/tex">A(x)</script>看做常数，对这个进行求导，得<script type="math/tex">G^\prime(F(x))=\frac{1}{F(x)}</script>
</p>
<p>那么代入牛顿迭代的公式得<script type="math/tex">F(x)\equiv F_0(x)-\frac{G(F_0(x))}{G'(F_0(x))}\pmod{x^n}</script>
</p>
<p>
<script type="math/tex">F(x)\equiv F_0(x)(1-\ln F_0(x)+A(x))\pmod{x^n}</script>
</p>
<p>(这里的<script type="math/tex">F_0(x)</script>是指在<script type="math/tex">\bmod x^{\frac n 2}</script>下的答案)</p>
<p>然后因为<script type="math/tex">A(0)=0</script>，所以<script type="math/tex">F(x)</script>的常数项为<script type="math/tex">1</script>
</p>
<div class="highlight"><pre><span></span><code><span class="linenos" data-linenos=" 1 "></span><span class="n">poly</span> <span class="nf">exp</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">,</span><span class="kt">int</span> <span class="n">k</span><span class="p">){</span>
<span class="linenos" data-linenos=" 2 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="n">k</span><span class="p">;</span><span class="n">a</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="linenos" data-linenos=" 3 "></span>    <span class="k">if</span><span class="p">(</span><span class="n">n</span><span class="o">==</span><span class="mi">1</span><span class="p">)</span><span class="k">return</span> <span class="n">one</span><span class="p">();</span>
<span class="linenos" data-linenos=" 4 "></span>    <span class="n">poly</span> <span class="n">f0</span><span class="o">=</span><span class="n">exp</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">k</span><span class="mi">-1</span><span class="p">);</span><span class="n">f0</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="linenos" data-linenos=" 5 "></span>    <span class="k">return</span> <span class="n">f0</span><span class="o">*</span><span class="p">(</span><span class="n">one</span><span class="p">()</span><span class="o">+</span><span class="n">a</span><span class="o">-</span><span class="n">ln</span><span class="p">(</span><span class="n">f0</span><span class="p">));</span> 
<span class="linenos" data-linenos=" 6 "></span><span class="p">}</span>
<span class="linenos" data-linenos=" 7 "></span><span class="n">poly</span> <span class="nf">exp</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">){</span>
<span class="linenos" data-linenos=" 8 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">();</span>
<span class="linenos" data-linenos=" 9 "></span>    <span class="n">a</span><span class="o">=</span><span class="n">exp</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">ext</span><span class="p">(</span><span class="n">n</span><span class="p">));</span><span class="n">a</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span><span class="k">return</span> <span class="n">a</span><span class="p">;</span>
<span class="linenos" data-linenos="10 "></span><span class="p">}</span>
</code></pre></div>
<h3 id="_7">多项式开根</h3>
<p>设<script type="math/tex">H^2(x)\equiv F(x)(\pmod\ x^{\frac n2})</script>
</p>
<p>
<script type="math/tex">G(x)\equiv H(x)(\pmod\ x^{\frac n2})</script>
</p>
<p>
<script type="math/tex">G(x)-H(x)\equiv 0(\pmod\ x^{\frac n2})</script>
</p>
<p>
<script type="math/tex">(G(x)-H(x))^2\equiv 0(\pmod\ x^{\frac n2})</script>
</p>
<p>
<script type="math/tex">G^2(x)-2H(x)*G(x)+H^2(x)\equiv 0(\pmod\ x^n)</script>
</p>
<p>
<script type="math/tex">F(x)-2H(x)*G(x)+H^2(x)\equiv 0(\pmod\ x^n)</script>
</p>
<p>
<script type="math/tex">G(x)=\frac {F(x)+H^2(x)}{2H(x)}</script>
</p>
<h4 id="a_01">
<script type="math/tex">a_0=1</script>
</h4>
<div class="highlight"><pre><span></span><code><span class="linenos" data-linenos=" 1 "></span><span class="n">poly</span> <span class="nf">sqrt</span><span class="p">(</span><span class="n">poly</span> <span class="n">F</span><span class="p">,</span><span class="kt">int</span> <span class="n">k</span><span class="p">){</span>
<span class="linenos" data-linenos=" 2 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="n">k</span><span class="p">;</span><span class="n">F</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="linenos" data-linenos=" 3 "></span>    <span class="k">if</span><span class="p">(</span><span class="n">n</span><span class="o">==</span><span class="mi">1</span><span class="p">){</span><span class="n">F</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span><span class="k">return</span> <span class="n">F</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 4 "></span>    <span class="n">poly</span> <span class="n">G</span><span class="p">,</span><span class="n">H</span><span class="o">=</span><span class="n">sqrt</span><span class="p">(</span><span class="n">F</span><span class="p">,</span><span class="n">k</span><span class="mi">-1</span><span class="p">);</span>
<span class="linenos" data-linenos=" 5 "></span>    <span class="n">H</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span><span class="n">init</span><span class="p">(</span><span class="n">k</span><span class="p">);</span><span class="n">poly</span> <span class="n">invH</span><span class="o">=</span><span class="n">inv</span><span class="p">(</span><span class="n">H</span><span class="p">);</span>
<span class="linenos" data-linenos=" 6 "></span>    <span class="n">G</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">),</span><span class="n">H</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="o">&lt;&lt;</span><span class="mi">1</span><span class="p">),</span><span class="n">F</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="o">&lt;&lt;</span><span class="mi">1</span><span class="p">);</span>
<span class="linenos" data-linenos=" 7 "></span>    <span class="n">init</span><span class="p">(</span><span class="n">k</span><span class="o">+</span><span class="mi">1</span><span class="p">),</span><span class="n">ntt</span><span class="p">(</span><span class="n">H</span><span class="p">,</span><span class="n">k</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">);</span>
<span class="linenos" data-linenos=" 8 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="p">(</span><span class="n">n</span><span class="o">&lt;&lt;</span><span class="mi">1</span><span class="p">);</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">H</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="n">H</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">*</span><span class="n">H</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
<span class="linenos" data-linenos=" 9 "></span>    <span class="n">ntt</span><span class="p">(</span><span class="n">H</span><span class="p">,</span><span class="n">k</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span><span class="mi">-1</span><span class="p">),</span><span class="n">H</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="linenos" data-linenos="10 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">G</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="n">H</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">+</span><span class="n">F</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
<span class="linenos" data-linenos="11 "></span>    <span class="n">G</span><span class="o">=</span><span class="n">G</span><span class="o">*</span><span class="n">invH</span><span class="p">;</span><span class="n">G</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span><span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">G</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">/=</span><span class="mi">2</span><span class="p">;</span>
<span class="linenos" data-linenos="12 "></span>    <span class="k">return</span> <span class="n">G</span><span class="p">;</span>
<span class="linenos" data-linenos="13 "></span><span class="p">}</span>
<span class="linenos" data-linenos="14 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="n">sqrt</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">){</span>
<span class="linenos" data-linenos="15 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">();</span>
<span class="linenos" data-linenos="16 "></span>    <span class="n">a</span><span class="o">=</span><span class="n">sqrt</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">ext</span><span class="p">(</span><span class="n">n</span><span class="p">)),</span><span class="n">a</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span><span class="k">return</span> <span class="n">a</span><span class="p">;;</span>
<span class="linenos" data-linenos="17 "></span><span class="p">}</span>
</code></pre></div>
<h4 id="a_2">
<script type="math/tex">a_0≠1</script>
</h4>
<p>套用模板中的<a href="/blog/2020/12/26/[营业日志]菜鸡の模板/#二次剩余">二次剩余</a>即可
<div class="highlight"><pre><span></span><code><span class="linenos" data-linenos=" 1 "></span><span class="k">namespace</span> <span class="n">residue</span><span class="p">{</span>
<span class="linenos" data-linenos=" 2 "></span>    <span class="c1">//求二次剩余</span>
<span class="linenos" data-linenos=" 3 "></span>    <span class="kt">int</span> <span class="n">I2</span><span class="p">;</span>
<span class="linenos" data-linenos=" 4 "></span>    <span class="k">struct</span> <span class="nc">complex</span><span class="p">{</span>
<span class="linenos" data-linenos=" 5 "></span>        <span class="kt">long</span> <span class="kt">long</span> <span class="n">real</span><span class="p">,</span><span class="n">imag</span><span class="p">;</span>
<span class="linenos" data-linenos=" 6 "></span>        <span class="n">complex</span><span class="o">&amp;</span><span class="k">operator</span><span class="o">=</span><span class="p">(</span><span class="kt">long</span> <span class="kt">long</span> <span class="n">x</span><span class="p">){</span><span class="n">real</span><span class="o">=</span><span class="n">x</span><span class="p">,</span><span class="n">imag</span><span class="o">=</span><span class="mi">0</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 7 "></span>        <span class="n">complex</span><span class="p">(</span><span class="kt">long</span> <span class="kt">long</span> <span class="n">real</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span><span class="kt">long</span> <span class="kt">long</span> <span class="n">imag</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span><span class="o">:</span><span class="n">real</span><span class="p">(</span><span class="n">real</span><span class="p">),</span><span class="n">imag</span><span class="p">(</span><span class="n">imag</span><span class="p">){}</span>
<span class="linenos" data-linenos=" 8 "></span>        <span class="kt">bool</span> <span class="k">operator</span><span class="o">==</span><span class="p">(</span><span class="k">const</span> <span class="n">complex</span><span class="o">&amp;</span><span class="n">b</span><span class="p">)</span><span class="k">const</span><span class="p">{</span><span class="k">return</span> <span class="n">real</span><span class="o">==</span><span class="n">b</span><span class="p">.</span><span class="n">real</span><span class="o">&amp;&amp;</span><span class="n">imag</span><span class="o">==</span><span class="n">b</span><span class="p">.</span><span class="n">imag</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 9 "></span>        <span class="n">complex</span> <span class="k">operator</span><span class="o">*</span><span class="p">(</span><span class="k">const</span> <span class="n">complex</span> <span class="o">&amp;</span><span class="n">b</span><span class="p">)</span><span class="k">const</span><span class="p">{</span><span class="k">return</span> <span class="nf">complex</span><span class="p">((</span><span class="n">real</span><span class="o">*</span><span class="n">b</span><span class="p">.</span><span class="n">real</span><span class="o">+</span><span class="n">I2</span><span class="o">*</span><span class="n">imag</span><span class="o">%</span><span class="n">mod</span><span class="o">*</span><span class="n">b</span><span class="p">.</span><span class="n">imag</span><span class="p">)</span><span class="o">%</span><span class="n">mod</span><span class="p">,(</span><span class="n">real</span><span class="o">*</span><span class="n">b</span><span class="p">.</span><span class="n">imag</span><span class="o">+</span><span class="n">imag</span><span class="o">*</span><span class="n">b</span><span class="p">.</span><span class="n">real</span><span class="p">)</span><span class="o">%</span><span class="n">mod</span><span class="p">);}</span>
<span class="linenos" data-linenos="10 "></span>    <span class="p">};</span>
<span class="linenos" data-linenos="11 "></span>    <span class="n">complex</span> <span class="nf">ksm</span><span class="p">(</span><span class="n">complex</span> <span class="n">a</span><span class="p">,</span><span class="kt">int</span> <span class="n">b</span><span class="p">){</span>
<span class="linenos" data-linenos="12 "></span>        <span class="n">complex</span> <span class="n">res</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span>
<span class="linenos" data-linenos="13 "></span>        <span class="k">while</span><span class="p">(</span><span class="n">b</span><span class="p">){</span>
<span class="linenos" data-linenos="14 "></span>            <span class="k">if</span><span class="p">(</span><span class="n">b</span><span class="o">&amp;</span><span class="mi">1</span><span class="p">)</span><span class="n">res</span><span class="o">=</span><span class="n">res</span><span class="o">*</span><span class="n">a</span><span class="p">;</span>
<span class="linenos" data-linenos="15 "></span>            <span class="n">a</span><span class="o">=</span><span class="n">a</span><span class="o">*</span><span class="n">a</span><span class="p">;</span><span class="n">b</span><span class="o">&gt;&gt;=</span><span class="mi">1</span><span class="p">;</span>
<span class="linenos" data-linenos="16 "></span>        <span class="p">}</span><span class="k">return</span> <span class="n">res</span><span class="p">;</span>
<span class="linenos" data-linenos="17 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="18 "></span>    <span class="kt">bool</span> <span class="nf">check</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">){</span><span class="k">return</span> <span class="n">ksm</span><span class="p">(</span><span class="n">complex</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="mi">0</span><span class="p">),(</span><span class="n">mod</span><span class="mi">-1</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span><span class="p">).</span><span class="n">real</span><span class="o">==</span><span class="mi">1</span><span class="p">;}</span>
<span class="linenos" data-linenos="19 "></span>    <span class="n">pair</span><span class="o">&lt;</span><span class="kt">int</span><span class="p">,</span><span class="kt">int</span><span class="o">&gt;</span><span class="n">solve</span><span class="p">(</span><span class="kt">int</span> <span class="n">n</span><span class="p">){</span>
<span class="linenos" data-linenos="20 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">n</span><span class="o">==</span><span class="mi">0</span><span class="p">)</span><span class="k">return</span> <span class="p">{</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">};</span>
<span class="linenos" data-linenos="21 "></span>        <span class="kt">long</span> <span class="kt">long</span> <span class="n">a</span><span class="o">=</span><span class="n">rand</span><span class="p">()</span><span class="o">%</span><span class="n">mod</span><span class="p">;</span>
<span class="linenos" data-linenos="22 "></span>        <span class="k">while</span><span class="p">(</span><span class="o">!</span><span class="n">a</span><span class="o">||</span><span class="n">check</span><span class="p">((</span><span class="n">a</span><span class="o">*</span><span class="n">a</span><span class="o">+</span><span class="n">mod</span><span class="o">-</span><span class="n">n</span><span class="p">)</span><span class="o">%</span><span class="n">mod</span><span class="p">))</span><span class="n">a</span><span class="o">=</span><span class="n">rand</span><span class="p">()</span><span class="o">%</span><span class="n">mod</span><span class="p">;</span>
<span class="linenos" data-linenos="23 "></span>        <span class="n">I2</span><span class="o">=</span><span class="p">(</span><span class="n">a</span><span class="o">*</span><span class="n">a</span><span class="o">+</span><span class="n">mod</span><span class="o">-</span><span class="n">n</span><span class="p">)</span><span class="o">%</span><span class="n">mod</span><span class="p">;</span>
<span class="linenos" data-linenos="24 "></span>        <span class="kt">int</span> <span class="n">x0</span><span class="o">=</span><span class="n">ksm</span><span class="p">(</span><span class="n">complex</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="mi">1</span><span class="p">),(</span><span class="n">mod</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span><span class="p">).</span><span class="n">real</span><span class="p">;</span>
<span class="linenos" data-linenos="25 "></span>        <span class="k">return</span> <span class="p">{</span><span class="n">min</span><span class="p">(</span><span class="n">x0</span><span class="p">,</span><span class="n">mod</span><span class="o">-</span><span class="n">x0</span><span class="p">),</span><span class="n">max</span><span class="p">(</span><span class="n">x0</span><span class="p">,</span><span class="n">mod</span><span class="o">-</span><span class="n">x0</span><span class="p">)};</span>
<span class="linenos" data-linenos="26 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="27 "></span><span class="p">}</span>
<span class="linenos" data-linenos="28 "></span><span class="n">poly</span> <span class="n">sqrt</span><span class="p">(</span><span class="n">poly</span> <span class="n">F</span><span class="p">,</span><span class="kt">int</span> <span class="n">k</span><span class="p">){</span>
<span class="linenos" data-linenos="29 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="n">k</span><span class="p">;</span><span class="n">F</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="linenos" data-linenos="30 "></span>    <span class="k">if</span><span class="p">(</span><span class="n">n</span><span class="o">==</span><span class="mi">1</span><span class="p">){</span><span class="n">F</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">=</span><span class="n">residue</span><span class="o">::</span><span class="n">solve</span><span class="p">(</span><span class="n">F</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="n">x</span><span class="p">).</span><span class="n">first</span><span class="p">;</span><span class="k">return</span> <span class="n">F</span><span class="p">;}</span>
<span class="linenos" data-linenos="31 "></span>    <span class="n">poly</span> <span class="n">G</span><span class="p">,</span><span class="n">H</span><span class="o">=</span><span class="n">sqrt</span><span class="p">(</span><span class="n">F</span><span class="p">,</span><span class="n">k</span><span class="mi">-1</span><span class="p">);</span>
<span class="linenos" data-linenos="32 "></span>    <span class="n">H</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span><span class="n">init</span><span class="p">(</span><span class="n">k</span><span class="p">);</span><span class="n">poly</span> <span class="n">invH</span><span class="o">=</span><span class="n">inv</span><span class="p">(</span><span class="n">H</span><span class="p">);</span>
<span class="linenos" data-linenos="33 "></span>    <span class="n">G</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">),</span><span class="n">H</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="o">&lt;&lt;</span><span class="mi">1</span><span class="p">),</span><span class="n">F</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="o">&lt;&lt;</span><span class="mi">1</span><span class="p">);</span>
<span class="linenos" data-linenos="34 "></span>    <span class="n">init</span><span class="p">(</span><span class="n">k</span><span class="o">+</span><span class="mi">1</span><span class="p">),</span><span class="n">ntt</span><span class="p">(</span><span class="n">H</span><span class="p">,</span><span class="n">k</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">);</span>
<span class="linenos" data-linenos="35 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="p">(</span><span class="n">n</span><span class="o">&lt;&lt;</span><span class="mi">1</span><span class="p">);</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">H</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="n">H</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">*</span><span class="n">H</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
<span class="linenos" data-linenos="36 "></span>    <span class="n">ntt</span><span class="p">(</span><span class="n">H</span><span class="p">,</span><span class="n">k</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span><span class="mi">-1</span><span class="p">),</span><span class="n">H</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="linenos" data-linenos="37 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">G</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="n">H</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">+</span><span class="n">F</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
<span class="linenos" data-linenos="38 "></span>    <span class="n">G</span><span class="o">=</span><span class="n">G</span><span class="o">*</span><span class="n">invH</span><span class="p">;</span><span class="n">G</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span><span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">G</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">/=</span><span class="mi">2</span><span class="p">;</span>
<span class="linenos" data-linenos="39 "></span>    <span class="k">return</span> <span class="n">G</span><span class="p">;</span>
<span class="linenos" data-linenos="40 "></span><span class="p">}</span>
<span class="linenos" data-linenos="41 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="n">sqrt</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">){</span>
<span class="linenos" data-linenos="42 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">();</span>
<span class="linenos" data-linenos="43 "></span>    <span class="n">a</span><span class="o">=</span><span class="n">sqrt</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">ext</span><span class="p">(</span><span class="n">n</span><span class="p">)),</span><span class="n">a</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span><span class="k">return</span> <span class="n">a</span><span class="p">;;</span>
<span class="linenos" data-linenos="44 "></span><span class="p">}</span>
</code></pre></div></p>
<h3 id="_8">多项式快速幂</h3>
<h4 id="a_3">
<script type="math/tex">a_0=1</script>
</h4>
<p>
<script type="math/tex">G(x)=(F(x))^k\pmod{x^n}</script>
</p>
<p>
<script type="math/tex">\ln G(x)=kF(x)\pmod{x^n}</script>
</p>
<p>
<script type="math/tex">G(x)=e^{kF(x)}\pmod{x^n}</script>
</p>
<div class="highlight"><pre><span></span><code><span class="linenos" data-linenos="1 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="n">pow</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">,</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">k</span><span class="p">){</span><span class="c1">//保证a[0]=1 </span>
<span class="linenos" data-linenos="2 "></span>    <span class="n">a</span><span class="o">=</span><span class="n">ln</span><span class="p">(</span><span class="n">a</span><span class="p">);</span><span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">();</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">*=</span><span class="n">k</span><span class="p">.</span><span class="n">x</span><span class="p">;</span>
<span class="linenos" data-linenos="3 "></span>    <span class="k">return</span> <span class="nf">exp</span><span class="p">(</span><span class="n">a</span><span class="p">);</span>
<span class="linenos" data-linenos="4 "></span><span class="p">}</span>
</code></pre></div>
<h4 id="a_4">
<script type="math/tex">a_0≠1</script>
</h4>
<p>把最低项改为<script type="math/tex">1</script>
</p>
<p>
<script type="math/tex">F(x)^k=\left(\frac{F(x)}{ax^t} \right)^k(ax)^{tk}</script>
</p>
<p>实现了一个<code>modpair</code>，因为<script type="math/tex">k</script>既要对<script type="math/tex">mod</script>取模，也要对<script type="math/tex">\varphi(mod)</script>取模</p>
<div class="highlight"><pre><span></span><code><span class="linenos" data-linenos=" 1 "></span><span class="k">struct</span> <span class="nc">modpair</span><span class="p">{</span>
<span class="linenos" data-linenos=" 2 "></span>    <span class="c1">//用于快速幂中的次数</span>
<span class="linenos" data-linenos=" 3 "></span>    <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span><span class="n">k1</span><span class="p">;</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="mi">-1</span><span class="o">&gt;</span><span class="n">k2</span><span class="p">;</span>
<span class="linenos" data-linenos=" 4 "></span>    <span class="k">struct</span> <span class="nc">trueint</span><span class="p">{</span>
<span class="linenos" data-linenos=" 5 "></span>        <span class="kt">double</span> <span class="n">lg</span><span class="p">;</span><span class="kt">int</span> <span class="n">x</span><span class="p">;</span>
<span class="linenos" data-linenos=" 6 "></span>        <span class="n">trueint</span> <span class="o">&amp;</span><span class="k">operator</span><span class="o">=</span><span class="p">(</span><span class="kt">int</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">x</span><span class="o">=</span><span class="n">o</span><span class="p">,</span><span class="n">lg</span><span class="o">=</span><span class="n">log10</span><span class="p">(</span><span class="n">o</span><span class="p">),</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 7 "></span>        <span class="n">trueint</span> <span class="o">&amp;</span><span class="k">operator</span><span class="o">+=</span><span class="p">(</span><span class="kt">int</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">lg</span><span class="o">&lt;=</span><span class="mi">8</span><span class="o">&amp;&amp;</span><span class="p">(</span><span class="n">x</span><span class="o">+=</span><span class="n">o</span><span class="p">,</span><span class="n">lg</span><span class="o">=</span><span class="n">log10</span><span class="p">(</span><span class="n">x</span><span class="p">)),</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 8 "></span>        <span class="n">trueint</span> <span class="o">&amp;</span><span class="k">operator</span><span class="o">*=</span><span class="p">(</span><span class="kt">int</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">x</span><span class="o">*=</span><span class="n">o</span><span class="p">,</span><span class="n">lg</span><span class="o">+=</span><span class="n">log10</span><span class="p">(</span><span class="n">o</span><span class="p">),</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 9 "></span>    <span class="p">}</span><span class="n">k3</span><span class="p">;</span>
<span class="linenos" data-linenos="10 "></span>    <span class="n">modpair</span><span class="p">(</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span><span class="n">_1</span><span class="p">,</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="mi">-1</span><span class="o">&gt;</span><span class="n">_2</span><span class="p">,</span><span class="n">trueint</span> <span class="n">_3</span><span class="p">)</span><span class="o">:</span><span class="n">k1</span><span class="p">(</span><span class="n">_1</span><span class="p">),</span><span class="n">k2</span><span class="p">(</span><span class="n">_2</span><span class="p">),</span><span class="n">k3</span><span class="p">(</span><span class="n">_3</span><span class="p">){}</span>
<span class="linenos" data-linenos="11 "></span>    <span class="n">modpair</span><span class="p">(</span><span class="kt">int</span> <span class="n">o</span><span class="o">=</span><span class="mi">0</span><span class="p">){</span><span class="n">k1</span><span class="o">=</span><span class="n">o</span><span class="p">,</span><span class="n">k2</span><span class="o">=</span><span class="n">o</span><span class="p">,</span><span class="n">k3</span><span class="o">=</span><span class="n">o</span><span class="p">;}</span>
<span class="linenos" data-linenos="12 "></span>    <span class="n">modpair</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">=</span> <span class="p">(</span><span class="kt">int</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">k1</span><span class="o">=</span><span class="n">o</span><span class="p">,</span><span class="n">k2</span><span class="o">=</span><span class="n">o</span><span class="p">,</span><span class="n">k3</span><span class="o">=</span><span class="n">o</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos="13 "></span>    <span class="n">modpair</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">+=</span><span class="p">(</span><span class="kt">int</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">k1</span><span class="o">+=</span><span class="n">o</span><span class="p">,</span><span class="n">k2</span><span class="o">+=</span><span class="n">o</span><span class="p">,</span><span class="n">k3</span><span class="o">+=</span><span class="n">o</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos="14 "></span>    <span class="n">modpair</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">*=</span><span class="p">(</span><span class="kt">int</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">k1</span><span class="o">*=</span><span class="n">o</span><span class="p">,</span><span class="n">k2</span><span class="o">*=</span><span class="n">o</span><span class="p">,</span><span class="n">k3</span><span class="o">*=</span><span class="n">o</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos="15 "></span>    <span class="k">friend</span> <span class="n">modpair</span> <span class="k">operator</span> <span class="o">+</span><span class="p">(</span><span class="n">modpair</span> <span class="n">a</span><span class="p">,</span><span class="kt">int</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">a</span><span class="o">+=</span><span class="n">o</span><span class="p">;}</span>
<span class="linenos" data-linenos="16 "></span>    <span class="k">friend</span> <span class="n">modpair</span> <span class="k">operator</span> <span class="o">*</span><span class="p">(</span><span class="n">modpair</span> <span class="n">a</span><span class="p">,</span><span class="kt">int</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">a</span><span class="o">*=</span><span class="n">o</span><span class="p">;}</span>
<span class="linenos" data-linenos="17 "></span>    <span class="n">modpair</span> <span class="k">operator</span><span class="o">-</span><span class="p">(){</span><span class="k">return</span> <span class="nf">modpair</span><span class="p">(</span><span class="n">k1</span><span class="p">,</span><span class="n">k2</span><span class="p">,</span><span class="n">k3</span><span class="p">);}</span>
<span class="linenos" data-linenos="18 "></span><span class="p">};</span>
<span class="linenos" data-linenos="19 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="n">pow2</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">,</span><span class="n">modpair</span> <span class="n">m</span><span class="p">){</span><span class="c1">//不保证a[0]=1 </span>
<span class="linenos" data-linenos="20 "></span>    <span class="kt">int</span> <span class="n">k</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">val</span><span class="p">;</span>
<span class="linenos" data-linenos="21 "></span>    <span class="k">while</span><span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="n">k</span><span class="p">]</span><span class="o">==</span><span class="mi">0</span><span class="o">&amp;&amp;</span><span class="n">k</span><span class="o">&lt;</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">())</span><span class="n">k</span><span class="o">++</span><span class="p">;</span>
<span class="linenos" data-linenos="22 "></span>    <span class="k">if</span><span class="p">(</span><span class="n">k</span><span class="o">==</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">()</span><span class="o">||</span><span class="n">k</span><span class="o">!=</span><span class="mi">0</span><span class="o">&amp;&amp;</span><span class="n">m</span><span class="p">.</span><span class="n">k3</span><span class="p">.</span><span class="n">lg</span><span class="o">&gt;</span><span class="mi">8</span><span class="o">||</span><span class="mf">1l</span><span class="n">l</span><span class="o">*</span><span class="n">m</span><span class="p">.</span><span class="n">k3</span><span class="p">.</span><span class="n">x</span><span class="o">*</span><span class="n">k</span><span class="o">&gt;=</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">()){</span>
<span class="linenos" data-linenos="23 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">();</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="k">return</span> <span class="n">a</span><span class="p">;}</span><span class="c1">//bye~</span>
<span class="linenos" data-linenos="24 "></span>    <span class="n">val</span><span class="o">=</span><span class="n">a</span><span class="p">[</span><span class="n">k</span><span class="p">];</span><span class="n">poly</span> <span class="n">b</span><span class="p">;</span><span class="n">b</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">()</span><span class="o">-</span><span class="n">k</span><span class="p">);</span>
<span class="linenos" data-linenos="25 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">b</span><span class="p">.</span><span class="n">size</span><span class="p">();</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">b</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="n">k</span><span class="p">]</span><span class="o">/</span><span class="n">val</span><span class="p">;</span>
<span class="linenos" data-linenos="26 "></span>    <span class="n">b</span><span class="o">=</span><span class="n">pow</span><span class="p">(</span><span class="n">b</span><span class="p">,</span><span class="n">m</span><span class="p">.</span><span class="n">k1</span><span class="p">);</span><span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">();</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="linenos" data-linenos="27 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">b</span><span class="p">.</span><span class="n">size</span><span class="p">()</span><span class="o">&amp;&amp;</span><span class="n">i</span><span class="o">+</span><span class="n">k</span><span class="o">*</span><span class="n">m</span><span class="p">.</span><span class="n">k1</span><span class="p">.</span><span class="n">x</span><span class="o">&lt;</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">();</span><span class="n">i</span><span class="o">++</span><span class="p">)</span>
<span class="linenos" data-linenos="28 "></span>        <span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="n">k</span><span class="o">*</span><span class="n">m</span><span class="p">.</span><span class="n">k1</span><span class="p">.</span><span class="n">x</span><span class="p">]</span><span class="o">=</span><span class="n">b</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">*</span><span class="p">(</span><span class="n">val</span><span class="o">^</span><span class="n">m</span><span class="p">.</span><span class="n">k2</span><span class="p">.</span><span class="n">x</span><span class="p">);</span>
<span class="linenos" data-linenos="29 "></span>    <span class="k">return</span> <span class="n">a</span><span class="p">;</span>
<span class="linenos" data-linenos="30 "></span><span class="p">}</span>
</code></pre></div>
<h3 id="_9">多项式三角函数</h3>
<p>由欧拉公式得到：</p>
<p>
<script type="math/tex; mode=display">\begin{cases}e^{iF(x)}=\cos(F(x))+i\sin(F(x))\\
e^{-iF(x)}=\cos(F(x))-i\sin(F(x))
\end{cases}</script>
</p>
<p>解方程得到
<script type="math/tex; mode=display">\cos F(x)=\frac{e^{iF(x)}+e^{-iF(x)}}{2}</script>
<script type="math/tex; mode=display">\sin F(x)=\frac{e^{iF(x)}-e^{-iF(x)}}{2i}</script>
</p>
<p>在取模条件下，<script type="math/tex">i=\sqrt{-1}=\sqrt{mod-1}</script>，即<script type="math/tex">mod-1</script>的二次剩余
<div class="highlight"><pre><span></span><code><span class="linenos" data-linenos="1 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="n">sin</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">){</span><span class="k">return</span> <span class="p">(</span><span class="n">exp</span><span class="p">(</span><span class="n">a</span><span class="o">*</span><span class="n">I</span><span class="p">)</span><span class="o">-</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">a</span><span class="o">*</span><span class="n">I</span><span class="p">))</span><span class="o">/</span><span class="p">(</span><span class="n">I</span><span class="o">*</span><span class="mi">2</span><span class="p">);}</span>
<span class="linenos" data-linenos="2 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="n">cos</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">){</span><span class="k">return</span> <span class="p">(</span><span class="n">exp</span><span class="p">(</span><span class="n">a</span><span class="o">*</span><span class="n">I</span><span class="p">)</span><span class="o">+</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">a</span><span class="o">*</span><span class="n">I</span><span class="p">))</span><span class="o">/</span><span class="mi">2</span><span class="p">;}</span>
</code></pre></div></p>
<h3 id="_10">多项式反三角函数</h3>
<p>
<script type="math/tex">G(x)\equiv\sin^{-1}F(x)\space (\text{mod }x^n)</script>
</p>
<p>
<script type="math/tex">G'(x)\equiv \frac{F'(x)}{\sqrt{1-F(x)^2}}\space ({\text{mod }x^n})</script>
</p>
<p>
<script type="math/tex">G(x)\equiv \int \frac{F'(x)}{\sqrt{1-F(x)^2}}\text dx\space ({\text{mod }x^n})</script>
</p>
<p>
<script type="math/tex">H(x)\equiv\tan^{-1}F(x)\space (\text{mod }x^n)</script>
</p>
<p>
<script type="math/tex">H'(x)\equiv\frac{F'(x)}{1+F(x)^2}\space (\text{mod }x^n)</script>
</p>
<p>
<script type="math/tex">H(x)\equiv\int\frac{F'(x)}{1+F(x)^2} \text dx\space (\text{mod }x^n)</script>
</p>
<div class="highlight"><pre><span></span><code><span class="linenos" data-linenos="1 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="n">asin</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">){</span>
<span class="linenos" data-linenos="2 "></span>    <span class="n">poly</span> <span class="n">G</span><span class="o">=-</span><span class="n">a</span><span class="o">*</span><span class="n">a</span><span class="p">;</span><span class="n">G</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">++</span><span class="p">;</span>
<span class="linenos" data-linenos="3 "></span>    <span class="k">return</span> <span class="nf">inter</span><span class="p">(</span><span class="n">deriv</span><span class="p">(</span><span class="n">a</span><span class="p">)</span><span class="o">*</span><span class="n">inv</span><span class="p">(</span><span class="n">sqrt</span><span class="p">(</span><span class="n">G</span><span class="p">)));</span>
<span class="linenos" data-linenos="4 "></span><span class="p">}</span>
<span class="linenos" data-linenos="5 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="n">atan</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">){</span>
<span class="linenos" data-linenos="6 "></span>    <span class="n">poly</span> <span class="n">G</span><span class="o">=</span><span class="n">a</span><span class="o">*</span><span class="n">a</span><span class="p">;</span><span class="n">G</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">++</span><span class="p">;</span>
<span class="linenos" data-linenos="7 "></span>    <span class="k">return</span> <span class="nf">inter</span><span class="p">(</span><span class="n">deriv</span><span class="p">(</span><span class="n">a</span><span class="p">)</span><span class="o">*</span><span class="n">inv</span><span class="p">(</span><span class="n">G</span><span class="p">));</span>
<span class="linenos" data-linenos="8 "></span><span class="p">}</span>
</code></pre></div>
<h3 id="_11">完整代码</h3>
<div><div class="fold_hider"><div class="Close hider_title">点击显/隐内容</div></div><div class="fold">
<div class="highlight"><pre><span></span><code><span class="linenos" data-linenos="  1 "></span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>
<span class="linenos" data-linenos="  2 "></span><span class="k">template</span><span class="o">&lt;</span><span class="k">const</span> <span class="kt">int</span> <span class="n">mod</span><span class="o">&gt;</span>
<span class="linenos" data-linenos="  3 "></span><span class="k">struct</span> <span class="nc">modint</span><span class="p">{</span>
<span class="linenos" data-linenos="  4 "></span>    <span class="kt">int</span> <span class="n">x</span><span class="p">;</span>
<span class="linenos" data-linenos="  5 "></span>    <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span><span class="p">(</span><span class="kt">int</span> <span class="n">o</span><span class="o">=</span><span class="mi">0</span><span class="p">){</span><span class="n">x</span><span class="o">=</span><span class="n">o</span><span class="p">;}</span>
<span class="linenos" data-linenos="  6 "></span>    <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">=</span> <span class="p">(</span><span class="kt">int</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">x</span><span class="o">=</span><span class="n">o</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos="  7 "></span>    <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">+=</span><span class="p">(</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">x</span><span class="o">=</span><span class="n">x</span><span class="o">+</span><span class="n">o</span><span class="p">.</span><span class="n">x</span><span class="o">&gt;=</span><span class="n">mod</span><span class="o">?</span><span class="n">x</span><span class="o">+</span><span class="n">o</span><span class="p">.</span><span class="n">x</span><span class="o">-</span><span class="nl">mod</span><span class="p">:</span><span class="n">x</span><span class="o">+</span><span class="n">o</span><span class="p">.</span><span class="n">x</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos="  8 "></span>    <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">-=</span><span class="p">(</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">x</span><span class="o">=</span><span class="n">x</span><span class="o">-</span><span class="n">o</span><span class="p">.</span><span class="n">x</span><span class="o">&lt;</span><span class="mi">0</span><span class="o">?</span><span class="n">x</span><span class="o">-</span><span class="n">o</span><span class="p">.</span><span class="n">x</span><span class="o">+</span><span class="nl">mod</span><span class="p">:</span><span class="n">x</span><span class="o">-</span><span class="n">o</span><span class="p">.</span><span class="n">x</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos="  9 "></span>    <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">*=</span><span class="p">(</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">x</span><span class="o">=</span><span class="mf">1l</span><span class="n">l</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">o</span><span class="p">.</span><span class="n">x</span><span class="o">%</span><span class="n">mod</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 10 "></span>    <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">^=</span><span class="p">(</span><span class="kt">int</span> <span class="n">b</span><span class="p">){</span>
<span class="linenos" data-linenos=" 11 "></span>        <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">a</span><span class="o">=*</span><span class="k">this</span><span class="p">,</span><span class="n">c</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span>
<span class="linenos" data-linenos=" 12 "></span>        <span class="k">for</span><span class="p">(;</span><span class="n">b</span><span class="p">;</span><span class="n">b</span><span class="o">&gt;&gt;=</span><span class="mi">1</span><span class="p">,</span><span class="n">a</span><span class="o">*=</span><span class="n">a</span><span class="p">)</span><span class="k">if</span><span class="p">(</span><span class="n">b</span><span class="o">&amp;</span><span class="mi">1</span><span class="p">)</span><span class="n">c</span><span class="o">*=</span><span class="n">a</span><span class="p">;</span>
<span class="linenos" data-linenos=" 13 "></span>        <span class="k">return</span> <span class="n">x</span><span class="o">=</span><span class="n">c</span><span class="p">.</span><span class="n">x</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;</span>
<span class="linenos" data-linenos=" 14 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos=" 15 "></span>    <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">/=</span><span class="p">(</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="o">*</span><span class="k">this</span> <span class="o">*=</span><span class="n">o</span><span class="o">^=</span><span class="n">mod</span><span class="mi">-2</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 16 "></span>    <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">+=</span><span class="p">(</span><span class="kt">int</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">x</span><span class="o">=</span><span class="n">x</span><span class="o">+</span><span class="n">o</span><span class="o">&gt;=</span><span class="n">mod</span><span class="o">?</span><span class="n">x</span><span class="o">+</span><span class="n">o</span><span class="o">-</span><span class="nl">mod</span><span class="p">:</span><span class="n">x</span><span class="o">+</span><span class="n">o</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 17 "></span>    <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">-=</span><span class="p">(</span><span class="kt">int</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">x</span><span class="o">=</span><span class="n">x</span><span class="o">-</span><span class="n">o</span><span class="o">&lt;</span><span class="mi">0</span><span class="o">?</span><span class="n">x</span><span class="o">-</span><span class="n">o</span><span class="o">+</span><span class="nl">mod</span><span class="p">:</span><span class="n">x</span><span class="o">-</span><span class="n">o</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 18 "></span>    <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">*=</span><span class="p">(</span><span class="kt">int</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">x</span><span class="o">=</span><span class="mf">1l</span><span class="n">l</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">o</span><span class="o">%</span><span class="n">mod</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 19 "></span>    <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">/=</span><span class="p">(</span><span class="kt">int</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="o">*</span><span class="k">this</span> <span class="o">*=</span> <span class="p">((</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span><span class="p">(</span><span class="n">o</span><span class="p">))</span><span class="o">^=</span><span class="n">mod</span><span class="mi">-2</span><span class="p">);}</span>
<span class="linenos" data-linenos=" 20 "></span>    <span class="k">template</span><span class="o">&lt;</span><span class="k">class</span> <span class="nc">I</span><span class="o">&gt;</span><span class="k">friend</span> <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="k">operator</span> <span class="o">+</span><span class="p">(</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">a</span><span class="p">,</span><span class="n">I</span> <span class="n">b</span><span class="p">){</span><span class="k">return</span> <span class="n">a</span><span class="o">+=</span><span class="n">b</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 21 "></span>    <span class="k">template</span><span class="o">&lt;</span><span class="k">class</span> <span class="nc">I</span><span class="o">&gt;</span><span class="k">friend</span> <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="k">operator</span> <span class="o">-</span><span class="p">(</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">a</span><span class="p">,</span><span class="n">I</span> <span class="n">b</span><span class="p">){</span><span class="k">return</span> <span class="n">a</span><span class="o">-=</span><span class="n">b</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 22 "></span>    <span class="k">template</span><span class="o">&lt;</span><span class="k">class</span> <span class="nc">I</span><span class="o">&gt;</span><span class="k">friend</span> <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="k">operator</span> <span class="o">*</span><span class="p">(</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">a</span><span class="p">,</span><span class="n">I</span> <span class="n">b</span><span class="p">){</span><span class="k">return</span> <span class="n">a</span><span class="o">*=</span><span class="n">b</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 23 "></span>    <span class="k">template</span><span class="o">&lt;</span><span class="k">class</span> <span class="nc">I</span><span class="o">&gt;</span><span class="k">friend</span> <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="k">operator</span> <span class="o">/</span><span class="p">(</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">a</span><span class="p">,</span><span class="n">I</span> <span class="n">b</span><span class="p">){</span><span class="k">return</span> <span class="n">a</span><span class="o">/=</span><span class="n">b</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 24 "></span>    <span class="k">friend</span> <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="k">operator</span> <span class="o">^</span><span class="p">(</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">a</span><span class="p">,</span><span class="kt">int</span> <span class="n">b</span><span class="p">){</span><span class="k">return</span> <span class="n">a</span><span class="o">^=</span><span class="n">b</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 25 "></span>    <span class="k">friend</span> <span class="kt">bool</span> <span class="k">operator</span> <span class="o">==</span><span class="p">(</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">a</span><span class="p">,</span><span class="kt">int</span> <span class="n">b</span><span class="p">){</span><span class="k">return</span> <span class="n">a</span><span class="p">.</span><span class="n">x</span><span class="o">==</span><span class="n">b</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 26 "></span>    <span class="k">friend</span> <span class="kt">bool</span> <span class="k">operator</span> <span class="o">!=</span><span class="p">(</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">a</span><span class="p">,</span><span class="kt">int</span> <span class="n">b</span><span class="p">){</span><span class="k">return</span> <span class="n">a</span><span class="p">.</span><span class="n">x</span><span class="o">!=</span><span class="n">b</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 27 "></span>    <span class="kt">bool</span> <span class="k">operator</span> <span class="o">!</span> <span class="p">()</span> <span class="p">{</span><span class="k">return</span> <span class="o">!</span><span class="n">x</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 28 "></span>    <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="k">operator</span> <span class="o">-</span> <span class="p">()</span> <span class="p">{</span><span class="k">return</span> <span class="n">x</span><span class="o">?</span><span class="n">mod</span><span class="o">-</span><span class="nl">x</span><span class="p">:</span><span class="mi">0</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 29 "></span>    <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="o">&amp;</span><span class="k">operator</span><span class="o">++</span><span class="p">(</span><span class="kt">int</span><span class="p">){</span><span class="k">return</span> <span class="o">*</span><span class="k">this</span><span class="o">+=</span><span class="mi">1</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 30 "></span><span class="p">};</span>
<span class="linenos" data-linenos=" 31 "></span><span class="k">const</span> <span class="kt">int</span> <span class="n">N</span><span class="o">=</span><span class="mf">4e6</span><span class="o">+</span><span class="mi">5</span><span class="p">;</span>
<span class="linenos" data-linenos=" 32 "></span>
<span class="linenos" data-linenos=" 33 "></span><span class="k">const</span> <span class="kt">int</span> <span class="n">mod</span><span class="o">=</span><span class="mi">998244353</span><span class="p">;</span>
<span class="linenos" data-linenos=" 34 "></span><span class="k">namespace</span> <span class="n">residue</span><span class="p">{</span>
<span class="linenos" data-linenos=" 35 "></span>    <span class="c1">//求二次剩余</span>
<span class="linenos" data-linenos=" 36 "></span>    <span class="kt">int</span> <span class="n">I2</span><span class="p">;</span>
<span class="linenos" data-linenos=" 37 "></span>    <span class="k">struct</span> <span class="nc">complex</span><span class="p">{</span>
<span class="linenos" data-linenos=" 38 "></span>        <span class="kt">long</span> <span class="kt">long</span> <span class="n">real</span><span class="p">,</span><span class="n">imag</span><span class="p">;</span>
<span class="linenos" data-linenos=" 39 "></span>        <span class="n">complex</span><span class="o">&amp;</span><span class="k">operator</span><span class="o">=</span><span class="p">(</span><span class="kt">long</span> <span class="kt">long</span> <span class="n">x</span><span class="p">){</span><span class="n">real</span><span class="o">=</span><span class="n">x</span><span class="p">,</span><span class="n">imag</span><span class="o">=</span><span class="mi">0</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 40 "></span>        <span class="n">complex</span><span class="p">(</span><span class="kt">long</span> <span class="kt">long</span> <span class="n">real</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span><span class="kt">long</span> <span class="kt">long</span> <span class="n">imag</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span><span class="o">:</span><span class="n">real</span><span class="p">(</span><span class="n">real</span><span class="p">),</span><span class="n">imag</span><span class="p">(</span><span class="n">imag</span><span class="p">){}</span>
<span class="linenos" data-linenos=" 41 "></span>        <span class="kt">bool</span> <span class="k">operator</span><span class="o">==</span><span class="p">(</span><span class="k">const</span> <span class="n">complex</span><span class="o">&amp;</span><span class="n">b</span><span class="p">)</span><span class="k">const</span><span class="p">{</span><span class="k">return</span> <span class="n">real</span><span class="o">==</span><span class="n">b</span><span class="p">.</span><span class="n">real</span><span class="o">&amp;&amp;</span><span class="n">imag</span><span class="o">==</span><span class="n">b</span><span class="p">.</span><span class="n">imag</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 42 "></span>        <span class="n">complex</span> <span class="k">operator</span><span class="o">*</span><span class="p">(</span><span class="k">const</span> <span class="n">complex</span> <span class="o">&amp;</span><span class="n">b</span><span class="p">)</span><span class="k">const</span><span class="p">{</span><span class="k">return</span> <span class="nf">complex</span><span class="p">((</span><span class="n">real</span><span class="o">*</span><span class="n">b</span><span class="p">.</span><span class="n">real</span><span class="o">+</span><span class="n">I2</span><span class="o">*</span><span class="n">imag</span><span class="o">%</span><span class="n">mod</span><span class="o">*</span><span class="n">b</span><span class="p">.</span><span class="n">imag</span><span class="p">)</span><span class="o">%</span><span class="n">mod</span><span class="p">,(</span><span class="n">real</span><span class="o">*</span><span class="n">b</span><span class="p">.</span><span class="n">imag</span><span class="o">+</span><span class="n">imag</span><span class="o">*</span><span class="n">b</span><span class="p">.</span><span class="n">real</span><span class="p">)</span><span class="o">%</span><span class="n">mod</span><span class="p">);}</span>
<span class="linenos" data-linenos=" 43 "></span>    <span class="p">};</span>
<span class="linenos" data-linenos=" 44 "></span>    <span class="n">complex</span> <span class="nf">ksm</span><span class="p">(</span><span class="n">complex</span> <span class="n">a</span><span class="p">,</span><span class="kt">int</span> <span class="n">b</span><span class="p">){</span>
<span class="linenos" data-linenos=" 45 "></span>        <span class="n">complex</span> <span class="n">res</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span>
<span class="linenos" data-linenos=" 46 "></span>        <span class="k">while</span><span class="p">(</span><span class="n">b</span><span class="p">){</span>
<span class="linenos" data-linenos=" 47 "></span>            <span class="k">if</span><span class="p">(</span><span class="n">b</span><span class="o">&amp;</span><span class="mi">1</span><span class="p">)</span><span class="n">res</span><span class="o">=</span><span class="n">res</span><span class="o">*</span><span class="n">a</span><span class="p">;</span>
<span class="linenos" data-linenos=" 48 "></span>            <span class="n">a</span><span class="o">=</span><span class="n">a</span><span class="o">*</span><span class="n">a</span><span class="p">;</span><span class="n">b</span><span class="o">&gt;&gt;=</span><span class="mi">1</span><span class="p">;</span>
<span class="linenos" data-linenos=" 49 "></span>        <span class="p">}</span><span class="k">return</span> <span class="n">res</span><span class="p">;</span>
<span class="linenos" data-linenos=" 50 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos=" 51 "></span>    <span class="kt">bool</span> <span class="nf">check</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">){</span><span class="k">return</span> <span class="n">ksm</span><span class="p">(</span><span class="n">complex</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="mi">0</span><span class="p">),(</span><span class="n">mod</span><span class="mi">-1</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span><span class="p">).</span><span class="n">real</span><span class="o">==</span><span class="mi">1</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 52 "></span>    <span class="n">pair</span><span class="o">&lt;</span><span class="kt">int</span><span class="p">,</span><span class="kt">int</span><span class="o">&gt;</span><span class="n">solve</span><span class="p">(</span><span class="kt">int</span> <span class="n">n</span><span class="p">){</span>
<span class="linenos" data-linenos=" 53 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">n</span><span class="o">==</span><span class="mi">0</span><span class="p">)</span><span class="k">return</span> <span class="p">{</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">};</span>
<span class="linenos" data-linenos=" 54 "></span>        <span class="kt">long</span> <span class="kt">long</span> <span class="n">a</span><span class="o">=</span><span class="n">rand</span><span class="p">()</span><span class="o">%</span><span class="n">mod</span><span class="p">;</span>
<span class="linenos" data-linenos=" 55 "></span>        <span class="k">while</span><span class="p">(</span><span class="o">!</span><span class="n">a</span><span class="o">||</span><span class="n">check</span><span class="p">((</span><span class="n">a</span><span class="o">*</span><span class="n">a</span><span class="o">+</span><span class="n">mod</span><span class="o">-</span><span class="n">n</span><span class="p">)</span><span class="o">%</span><span class="n">mod</span><span class="p">))</span><span class="n">a</span><span class="o">=</span><span class="n">rand</span><span class="p">()</span><span class="o">%</span><span class="n">mod</span><span class="p">;</span>
<span class="linenos" data-linenos=" 56 "></span>        <span class="n">I2</span><span class="o">=</span><span class="p">(</span><span class="n">a</span><span class="o">*</span><span class="n">a</span><span class="o">+</span><span class="n">mod</span><span class="o">-</span><span class="n">n</span><span class="p">)</span><span class="o">%</span><span class="n">mod</span><span class="p">;</span>
<span class="linenos" data-linenos=" 57 "></span>        <span class="kt">int</span> <span class="n">x0</span><span class="o">=</span><span class="n">ksm</span><span class="p">(</span><span class="n">complex</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="mi">1</span><span class="p">),(</span><span class="n">mod</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span><span class="p">).</span><span class="n">real</span><span class="p">;</span>
<span class="linenos" data-linenos=" 58 "></span>        <span class="k">return</span> <span class="p">{</span><span class="n">min</span><span class="p">(</span><span class="n">x0</span><span class="p">,</span><span class="n">mod</span><span class="o">-</span><span class="n">x0</span><span class="p">),</span><span class="n">max</span><span class="p">(</span><span class="n">x0</span><span class="p">,</span><span class="n">mod</span><span class="o">-</span><span class="n">x0</span><span class="p">)};</span>
<span class="linenos" data-linenos=" 59 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos=" 60 "></span><span class="p">}</span>
<span class="linenos" data-linenos=" 61 "></span><span class="k">const</span> <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">GG</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span><span class="n">Ginv</span><span class="o">=</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="mi">3</span><span class="p">,</span><span class="n">I</span><span class="o">=</span><span class="mi">86583718</span><span class="p">;</span>
<span class="linenos" data-linenos=" 62 "></span><span class="k">struct</span> <span class="nc">poly</span><span class="p">{</span>
<span class="linenos" data-linenos=" 63 "></span>    <span class="n">vector</span><span class="o">&lt;</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;&gt;</span><span class="n">a</span><span class="p">;</span>
<span class="linenos" data-linenos=" 64 "></span>    <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;&amp;</span><span class="k">operator</span><span class="p">[](</span><span class="kt">int</span> <span class="n">i</span><span class="p">){</span><span class="k">return</span> <span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">];}</span>
<span class="linenos" data-linenos=" 65 "></span>    <span class="kt">int</span> <span class="n">size</span><span class="p">(){</span><span class="k">return</span> <span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">();}</span>
<span class="linenos" data-linenos=" 66 "></span>    <span class="kt">void</span> <span class="n">resize</span><span class="p">(</span><span class="kt">int</span> <span class="n">n</span><span class="p">){</span><span class="n">a</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);}</span>
<span class="linenos" data-linenos=" 67 "></span>    <span class="kt">void</span> <span class="n">reverse</span><span class="p">(){</span><span class="n">std</span><span class="o">::</span><span class="n">reverse</span><span class="p">(</span><span class="n">a</span><span class="p">.</span><span class="n">begin</span><span class="p">(),</span><span class="n">a</span><span class="p">.</span><span class="n">end</span><span class="p">());}</span>
<span class="linenos" data-linenos=" 68 "></span><span class="p">};</span>
<span class="linenos" data-linenos=" 69 "></span><span class="kt">int</span> <span class="n">rev</span><span class="p">[</span><span class="n">N</span><span class="p">];</span>
<span class="linenos" data-linenos=" 70 "></span><span class="kr">inline</span> <span class="kt">int</span> <span class="n">ext</span><span class="p">(</span><span class="kt">int</span> <span class="n">n</span><span class="p">){</span><span class="kt">int</span> <span class="n">k</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="k">while</span><span class="p">((</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="n">k</span><span class="p">)</span><span class="o">&lt;</span><span class="n">n</span><span class="p">)</span><span class="n">k</span><span class="o">++</span><span class="p">;</span><span class="k">return</span> <span class="n">k</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 71 "></span><span class="kr">inline</span> <span class="kt">void</span> <span class="n">init</span><span class="p">(</span><span class="kt">int</span> <span class="n">k</span><span class="p">){</span><span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="n">k</span><span class="p">;</span><span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">rev</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="p">(</span><span class="n">rev</span><span class="p">[</span><span class="n">i</span><span class="o">&gt;&gt;</span><span class="mi">1</span><span class="p">]</span><span class="o">&gt;&gt;</span><span class="mi">1</span><span class="p">)</span><span class="o">|</span><span class="p">((</span><span class="n">i</span><span class="o">&amp;</span><span class="mi">1</span><span class="p">)</span><span class="o">&lt;&lt;</span><span class="p">(</span><span class="n">k</span><span class="mi">-1</span><span class="p">));}</span>
<span class="linenos" data-linenos=" 72 "></span><span class="kr">inline</span> <span class="kt">void</span> <span class="n">ntt</span><span class="p">(</span><span class="n">poly</span><span class="o">&amp;</span><span class="n">a</span><span class="p">,</span><span class="kt">int</span> <span class="n">k</span><span class="p">,</span><span class="kt">int</span> <span class="n">typ</span><span class="p">){</span>
<span class="linenos" data-linenos=" 73 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="n">k</span><span class="p">;</span>
<span class="linenos" data-linenos=" 74 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="k">if</span><span class="p">(</span><span class="n">i</span><span class="o">&lt;</span><span class="n">rev</span><span class="p">[</span><span class="n">i</span><span class="p">])</span><span class="n">swap</span><span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">],</span><span class="n">a</span><span class="p">[</span><span class="n">rev</span><span class="p">[</span><span class="n">i</span><span class="p">]]);</span>
<span class="linenos" data-linenos=" 75 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">mid</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span><span class="n">mid</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">mid</span><span class="o">&lt;&lt;=</span><span class="mi">1</span><span class="p">){</span>
<span class="linenos" data-linenos=" 76 "></span>        <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">wn</span><span class="o">=</span><span class="p">(</span><span class="n">typ</span><span class="o">&gt;</span><span class="mi">0</span><span class="o">?</span><span class="nl">GG</span><span class="p">:</span><span class="n">Ginv</span><span class="p">)</span><span class="o">^</span><span class="p">((</span><span class="n">mod</span><span class="mi">-1</span><span class="p">)</span><span class="o">/</span><span class="p">(</span><span class="n">mid</span><span class="o">&lt;&lt;</span><span class="mi">1</span><span class="p">));</span>
<span class="linenos" data-linenos=" 77 "></span>        <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">r</span><span class="o">=</span><span class="n">mid</span><span class="o">&lt;&lt;</span><span class="mi">1</span><span class="p">,</span><span class="n">j</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">j</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">j</span><span class="o">+=</span><span class="n">r</span><span class="p">){</span>
<span class="linenos" data-linenos=" 78 "></span>            <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">w</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span>
<span class="linenos" data-linenos=" 79 "></span>            <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">k</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">k</span><span class="o">&lt;</span><span class="n">mid</span><span class="p">;</span><span class="n">k</span><span class="o">++</span><span class="p">,</span><span class="n">w</span><span class="o">=</span><span class="n">w</span><span class="o">*</span><span class="n">wn</span><span class="p">){</span>
<span class="linenos" data-linenos=" 80 "></span>                <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">x</span><span class="o">=</span><span class="n">a</span><span class="p">[</span><span class="n">j</span><span class="o">+</span><span class="n">k</span><span class="p">],</span><span class="n">y</span><span class="o">=</span><span class="n">w</span><span class="o">*</span><span class="n">a</span><span class="p">[</span><span class="n">j</span><span class="o">+</span><span class="n">k</span><span class="o">+</span><span class="n">mid</span><span class="p">];</span>
<span class="linenos" data-linenos=" 81 "></span>                <span class="n">a</span><span class="p">[</span><span class="n">j</span><span class="o">+</span><span class="n">k</span><span class="p">]</span><span class="o">=</span><span class="n">x</span><span class="o">+</span><span class="n">y</span><span class="p">,</span><span class="n">a</span><span class="p">[</span><span class="n">j</span><span class="o">+</span><span class="n">k</span><span class="o">+</span><span class="n">mid</span><span class="p">]</span><span class="o">=</span><span class="n">x</span><span class="o">-</span><span class="n">y</span><span class="p">;</span>
<span class="linenos" data-linenos=" 82 "></span>            <span class="p">}</span>
<span class="linenos" data-linenos=" 83 "></span>        <span class="p">}</span>
<span class="linenos" data-linenos=" 84 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos=" 85 "></span>    <span class="k">if</span><span class="p">(</span><span class="n">typ</span><span class="o">&lt;</span><span class="mi">0</span><span class="p">){</span>
<span class="linenos" data-linenos=" 86 "></span>        <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">inv</span><span class="o">=</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="n">n</span><span class="p">;</span>
<span class="linenos" data-linenos=" 87 "></span>        <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">*=</span><span class="n">inv</span><span class="p">;</span>
<span class="linenos" data-linenos=" 88 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos=" 89 "></span><span class="p">}</span>
<span class="linenos" data-linenos=" 90 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="n">one</span><span class="p">(){</span><span class="n">poly</span> <span class="n">a</span><span class="p">;</span><span class="n">a</span><span class="p">.</span><span class="n">a</span><span class="p">.</span><span class="n">push_back</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span><span class="k">return</span> <span class="n">a</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 91 "></span><span class="n">poly</span> <span class="k">operator</span> <span class="o">+</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">,</span><span class="n">poly</span> <span class="n">b</span><span class="p">){</span>
<span class="linenos" data-linenos=" 92 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="n">max</span><span class="p">(</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">(),</span><span class="n">b</span><span class="p">.</span><span class="n">size</span><span class="p">());</span><span class="n">a</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">),</span><span class="n">b</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="linenos" data-linenos=" 93 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">+=</span><span class="n">b</span><span class="p">[</span><span class="n">i</span><span class="p">];</span><span class="k">return</span> <span class="n">a</span><span class="p">;</span>
<span class="linenos" data-linenos=" 94 "></span><span class="p">}</span>
<span class="linenos" data-linenos=" 95 "></span><span class="n">poly</span> <span class="k">operator</span> <span class="o">-</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">,</span><span class="n">poly</span> <span class="n">b</span><span class="p">){</span>
<span class="linenos" data-linenos=" 96 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="n">max</span><span class="p">(</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">(),</span><span class="n">b</span><span class="p">.</span><span class="n">size</span><span class="p">());</span><span class="n">a</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">),</span><span class="n">b</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="linenos" data-linenos=" 97 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-=</span><span class="n">b</span><span class="p">[</span><span class="n">i</span><span class="p">];</span><span class="k">return</span> <span class="n">a</span><span class="p">;</span>
<span class="linenos" data-linenos=" 98 "></span><span class="p">}</span>
<span class="linenos" data-linenos=" 99 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="k">operator</span><span class="o">*</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">,</span><span class="n">poly</span> <span class="n">b</span><span class="p">){</span>
<span class="linenos" data-linenos="100 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">()</span><span class="o">+</span><span class="n">b</span><span class="p">.</span><span class="n">size</span><span class="p">()</span><span class="mi">-1</span><span class="p">,</span><span class="n">k</span><span class="o">=</span><span class="n">ext</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="linenos" data-linenos="101 "></span>    <span class="n">a</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="n">k</span><span class="p">),</span><span class="n">b</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="n">k</span><span class="p">),</span><span class="n">init</span><span class="p">(</span><span class="n">k</span><span class="p">);</span>
<span class="linenos" data-linenos="102 "></span>    <span class="n">ntt</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">k</span><span class="p">,</span><span class="mi">1</span><span class="p">);</span><span class="n">ntt</span><span class="p">(</span><span class="n">b</span><span class="p">,</span><span class="n">k</span><span class="p">,</span><span class="mi">1</span><span class="p">);</span><span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="p">(</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="n">k</span><span class="p">);</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">*=</span><span class="n">b</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
<span class="linenos" data-linenos="103 "></span>    <span class="n">ntt</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">k</span><span class="p">,</span><span class="mi">-1</span><span class="p">),</span><span class="n">a</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span><span class="k">return</span> <span class="n">a</span><span class="p">;</span>
<span class="linenos" data-linenos="104 "></span><span class="p">}</span>
<span class="linenos" data-linenos="105 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="k">operator</span><span class="o">*</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">,</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">b</span><span class="p">){</span><span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">();</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">*=</span><span class="n">b</span><span class="p">;</span><span class="k">return</span> <span class="n">a</span><span class="p">;</span> <span class="p">}</span>
<span class="linenos" data-linenos="106 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="k">operator</span><span class="o">/</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">,</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">b</span><span class="p">){</span><span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">();</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">/=</span><span class="n">b</span><span class="p">;</span><span class="k">return</span> <span class="n">a</span><span class="p">;</span> <span class="p">}</span>
<span class="linenos" data-linenos="107 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="k">operator</span><span class="o">-</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">){</span><span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">();</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=-</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">];</span><span class="k">return</span> <span class="n">a</span><span class="p">;</span> <span class="p">}</span>
<span class="linenos" data-linenos="108 "></span><span class="n">poly</span> <span class="n">inv</span><span class="p">(</span><span class="n">poly</span> <span class="n">F</span><span class="p">,</span><span class="kt">int</span> <span class="n">k</span><span class="p">){</span>
<span class="linenos" data-linenos="109 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="n">k</span><span class="p">;</span><span class="n">F</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="linenos" data-linenos="110 "></span>    <span class="k">if</span><span class="p">(</span><span class="n">n</span><span class="o">==</span><span class="mi">1</span><span class="p">){</span><span class="n">F</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">=</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="n">F</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span><span class="k">return</span> <span class="n">F</span><span class="p">;}</span>
<span class="linenos" data-linenos="111 "></span>    <span class="n">poly</span> <span class="n">G</span><span class="p">,</span><span class="n">H</span><span class="o">=</span><span class="n">inv</span><span class="p">(</span><span class="n">F</span><span class="p">,</span><span class="n">k</span><span class="mi">-1</span><span class="p">);</span>
<span class="linenos" data-linenos="112 "></span>    <span class="n">G</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">),</span><span class="n">H</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="o">&lt;&lt;</span><span class="mi">1</span><span class="p">),</span><span class="n">F</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="o">&lt;&lt;</span><span class="mi">1</span><span class="p">);</span>
<span class="linenos" data-linenos="113 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="o">/</span><span class="mi">2</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">G</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="n">H</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">*</span><span class="mi">2</span><span class="p">;</span>
<span class="linenos" data-linenos="114 "></span>    <span class="n">init</span><span class="p">(</span><span class="n">k</span><span class="o">+</span><span class="mi">1</span><span class="p">),</span><span class="n">ntt</span><span class="p">(</span><span class="n">H</span><span class="p">,</span><span class="n">k</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">),</span><span class="n">ntt</span><span class="p">(</span><span class="n">F</span><span class="p">,</span><span class="n">k</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">);</span>
<span class="linenos" data-linenos="115 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="p">(</span><span class="n">n</span><span class="o">&lt;&lt;</span><span class="mi">1</span><span class="p">);</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">H</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="n">H</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">*</span><span class="n">H</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">*</span><span class="n">F</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
<span class="linenos" data-linenos="116 "></span>    <span class="n">ntt</span><span class="p">(</span><span class="n">H</span><span class="p">,</span><span class="n">k</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span><span class="mi">-1</span><span class="p">),</span><span class="n">H</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="linenos" data-linenos="117 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">G</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-=</span><span class="n">H</span><span class="p">[</span><span class="n">i</span><span class="p">];</span><span class="k">return</span> <span class="n">G</span><span class="p">;</span>
<span class="linenos" data-linenos="118 "></span><span class="p">}</span>
<span class="linenos" data-linenos="119 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="n">inv</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">){</span>
<span class="linenos" data-linenos="120 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">();</span>
<span class="linenos" data-linenos="121 "></span>    <span class="n">a</span><span class="o">=</span><span class="n">inv</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">ext</span><span class="p">(</span><span class="n">n</span><span class="p">)),</span><span class="n">a</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span><span class="k">return</span> <span class="n">a</span><span class="p">;;</span>
<span class="linenos" data-linenos="122 "></span><span class="p">}</span>
<span class="linenos" data-linenos="123 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="n">deriv</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">){</span><span class="c1">//求导 </span>
<span class="linenos" data-linenos="124 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">()</span><span class="mi">-1</span><span class="p">;</span>
<span class="linenos" data-linenos="125 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span><span class="o">*</span><span class="p">(</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">);</span>
<span class="linenos" data-linenos="126 "></span>    <span class="n">a</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span><span class="k">return</span> <span class="n">a</span><span class="p">;</span>
<span class="linenos" data-linenos="127 "></span><span class="p">}</span>
<span class="linenos" data-linenos="128 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="n">inter</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">){</span><span class="c1">//求原 </span>
<span class="linenos" data-linenos="129 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">()</span><span class="o">+</span><span class="mi">1</span><span class="p">;</span><span class="n">a</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="linenos" data-linenos="130 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">&gt;=</span><span class="mi">1</span><span class="p">;</span><span class="n">i</span><span class="o">--</span><span class="p">)</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="mi">-1</span><span class="p">]</span><span class="o">/</span><span class="n">i</span><span class="p">;</span>
<span class="linenos" data-linenos="131 "></span>    <span class="n">a</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="k">return</span> <span class="n">a</span><span class="p">;</span>
<span class="linenos" data-linenos="132 "></span><span class="p">}</span>
<span class="linenos" data-linenos="133 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="n">ln</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">){</span>
<span class="linenos" data-linenos="134 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">();</span>
<span class="linenos" data-linenos="135 "></span>    <span class="n">a</span><span class="o">=</span><span class="n">inter</span><span class="p">(</span><span class="n">deriv</span><span class="p">(</span><span class="n">a</span><span class="p">)</span><span class="o">*</span><span class="n">inv</span><span class="p">(</span><span class="n">a</span><span class="p">));</span>
<span class="linenos" data-linenos="136 "></span>    <span class="n">a</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span><span class="k">return</span> <span class="n">a</span><span class="p">;</span>
<span class="linenos" data-linenos="137 "></span><span class="p">}</span>
<span class="linenos" data-linenos="138 "></span><span class="n">poly</span> <span class="n">exp</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">,</span><span class="kt">int</span> <span class="n">k</span><span class="p">){</span>
<span class="linenos" data-linenos="139 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="n">k</span><span class="p">;</span><span class="n">a</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="linenos" data-linenos="140 "></span>    <span class="k">if</span><span class="p">(</span><span class="n">n</span><span class="o">==</span><span class="mi">1</span><span class="p">)</span><span class="k">return</span> <span class="n">one</span><span class="p">();</span>
<span class="linenos" data-linenos="141 "></span>    <span class="n">poly</span> <span class="n">f0</span><span class="o">=</span><span class="n">exp</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">k</span><span class="mi">-1</span><span class="p">);</span><span class="n">f0</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="linenos" data-linenos="142 "></span>    <span class="k">return</span> <span class="n">f0</span><span class="o">*</span><span class="p">(</span><span class="n">one</span><span class="p">()</span><span class="o">+</span><span class="n">a</span><span class="o">-</span><span class="n">ln</span><span class="p">(</span><span class="n">f0</span><span class="p">));</span> 
<span class="linenos" data-linenos="143 "></span><span class="p">}</span>
<span class="linenos" data-linenos="144 "></span><span class="n">poly</span> <span class="n">exp</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">){</span>
<span class="linenos" data-linenos="145 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">();</span>
<span class="linenos" data-linenos="146 "></span>    <span class="n">a</span><span class="o">=</span><span class="n">exp</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">ext</span><span class="p">(</span><span class="n">n</span><span class="p">));</span><span class="n">a</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span><span class="k">return</span> <span class="n">a</span><span class="p">;</span>
<span class="linenos" data-linenos="147 "></span><span class="p">}</span>
<span class="linenos" data-linenos="148 "></span><span class="n">poly</span> <span class="n">sqrt</span><span class="p">(</span><span class="n">poly</span> <span class="n">F</span><span class="p">,</span><span class="kt">int</span> <span class="n">k</span><span class="p">){</span>
<span class="linenos" data-linenos="149 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="n">k</span><span class="p">;</span><span class="n">F</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="linenos" data-linenos="150 "></span>    <span class="k">if</span><span class="p">(</span><span class="n">n</span><span class="o">==</span><span class="mi">1</span><span class="p">){</span><span class="n">F</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">=</span><span class="n">residue</span><span class="o">::</span><span class="n">solve</span><span class="p">(</span><span class="n">F</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="n">x</span><span class="p">).</span><span class="n">first</span><span class="p">;</span><span class="k">return</span> <span class="n">F</span><span class="p">;}</span>
<span class="linenos" data-linenos="151 "></span>    <span class="n">poly</span> <span class="n">G</span><span class="p">,</span><span class="n">H</span><span class="o">=</span><span class="n">sqrt</span><span class="p">(</span><span class="n">F</span><span class="p">,</span><span class="n">k</span><span class="mi">-1</span><span class="p">);</span>
<span class="linenos" data-linenos="152 "></span>    <span class="n">H</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span><span class="n">init</span><span class="p">(</span><span class="n">k</span><span class="p">);</span><span class="n">poly</span> <span class="n">invH</span><span class="o">=</span><span class="n">inv</span><span class="p">(</span><span class="n">H</span><span class="p">);</span>
<span class="linenos" data-linenos="153 "></span>    <span class="n">G</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">),</span><span class="n">H</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="o">&lt;&lt;</span><span class="mi">1</span><span class="p">),</span><span class="n">F</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="o">&lt;&lt;</span><span class="mi">1</span><span class="p">);</span>
<span class="linenos" data-linenos="154 "></span>    <span class="n">init</span><span class="p">(</span><span class="n">k</span><span class="o">+</span><span class="mi">1</span><span class="p">),</span><span class="n">ntt</span><span class="p">(</span><span class="n">H</span><span class="p">,</span><span class="n">k</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">);</span>
<span class="linenos" data-linenos="155 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="p">(</span><span class="n">n</span><span class="o">&lt;&lt;</span><span class="mi">1</span><span class="p">);</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">H</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="n">H</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">*</span><span class="n">H</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
<span class="linenos" data-linenos="156 "></span>    <span class="n">ntt</span><span class="p">(</span><span class="n">H</span><span class="p">,</span><span class="n">k</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span><span class="mi">-1</span><span class="p">),</span><span class="n">H</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="linenos" data-linenos="157 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">G</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="n">H</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">+</span><span class="n">F</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
<span class="linenos" data-linenos="158 "></span>    <span class="n">G</span><span class="o">=</span><span class="n">G</span><span class="o">*</span><span class="n">invH</span><span class="p">;</span><span class="n">G</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span><span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">G</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">/=</span><span class="mi">2</span><span class="p">;</span>
<span class="linenos" data-linenos="159 "></span>    <span class="k">return</span> <span class="n">G</span><span class="p">;</span>
<span class="linenos" data-linenos="160 "></span><span class="p">}</span>
<span class="linenos" data-linenos="161 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="n">sqrt</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">){</span>
<span class="linenos" data-linenos="162 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">();</span>
<span class="linenos" data-linenos="163 "></span>    <span class="n">a</span><span class="o">=</span><span class="n">sqrt</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">ext</span><span class="p">(</span><span class="n">n</span><span class="p">)),</span><span class="n">a</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span><span class="k">return</span> <span class="n">a</span><span class="p">;;</span>
<span class="linenos" data-linenos="164 "></span><span class="p">}</span>
<span class="linenos" data-linenos="165 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="n">pow</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">,</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">k</span><span class="p">){</span><span class="c1">//保证a[0]=1 </span>
<span class="linenos" data-linenos="166 "></span>    <span class="n">a</span><span class="o">=</span><span class="n">ln</span><span class="p">(</span><span class="n">a</span><span class="p">);</span><span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">();</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">*=</span><span class="n">k</span><span class="p">.</span><span class="n">x</span><span class="p">;</span>
<span class="linenos" data-linenos="167 "></span>    <span class="k">return</span> <span class="nf">exp</span><span class="p">(</span><span class="n">a</span><span class="p">);</span>
<span class="linenos" data-linenos="168 "></span><span class="p">}</span>
<span class="linenos" data-linenos="169 "></span>
<span class="linenos" data-linenos="170 "></span><span class="k">struct</span> <span class="nc">modpair</span><span class="p">{</span>
<span class="linenos" data-linenos="171 "></span>    <span class="c1">//用于快速幂中的次数</span>
<span class="linenos" data-linenos="172 "></span>    <span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span><span class="n">k1</span><span class="p">;</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="mi">-1</span><span class="o">&gt;</span><span class="n">k2</span><span class="p">;</span>
<span class="linenos" data-linenos="173 "></span>    <span class="k">struct</span> <span class="nc">trueint</span><span class="p">{</span>
<span class="linenos" data-linenos="174 "></span>        <span class="kt">double</span> <span class="n">lg</span><span class="p">;</span><span class="kt">int</span> <span class="n">x</span><span class="p">;</span>
<span class="linenos" data-linenos="175 "></span>        <span class="n">trueint</span> <span class="o">&amp;</span><span class="k">operator</span><span class="o">=</span><span class="p">(</span><span class="kt">int</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">x</span><span class="o">=</span><span class="n">o</span><span class="p">,</span><span class="n">lg</span><span class="o">=</span><span class="n">log10</span><span class="p">(</span><span class="n">o</span><span class="p">),</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos="176 "></span>        <span class="n">trueint</span> <span class="o">&amp;</span><span class="k">operator</span><span class="o">+=</span><span class="p">(</span><span class="kt">int</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">lg</span><span class="o">&lt;=</span><span class="mi">8</span><span class="o">&amp;&amp;</span><span class="p">(</span><span class="n">x</span><span class="o">+=</span><span class="n">o</span><span class="p">,</span><span class="n">lg</span><span class="o">=</span><span class="n">log10</span><span class="p">(</span><span class="n">x</span><span class="p">)),</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos="177 "></span>        <span class="n">trueint</span> <span class="o">&amp;</span><span class="k">operator</span><span class="o">*=</span><span class="p">(</span><span class="kt">int</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">x</span><span class="o">*=</span><span class="n">o</span><span class="p">,</span><span class="n">lg</span><span class="o">+=</span><span class="n">log10</span><span class="p">(</span><span class="n">o</span><span class="p">),</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos="178 "></span>    <span class="p">}</span><span class="n">k3</span><span class="p">;</span>
<span class="linenos" data-linenos="179 "></span>    <span class="n">modpair</span><span class="p">(</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span><span class="n">_1</span><span class="p">,</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="mi">-1</span><span class="o">&gt;</span><span class="n">_2</span><span class="p">,</span><span class="n">trueint</span> <span class="n">_3</span><span class="p">)</span><span class="o">:</span><span class="n">k1</span><span class="p">(</span><span class="n">_1</span><span class="p">),</span><span class="n">k2</span><span class="p">(</span><span class="n">_2</span><span class="p">),</span><span class="n">k3</span><span class="p">(</span><span class="n">_3</span><span class="p">){}</span>
<span class="linenos" data-linenos="180 "></span>    <span class="n">modpair</span><span class="p">(</span><span class="kt">int</span> <span class="n">o</span><span class="o">=</span><span class="mi">0</span><span class="p">){</span><span class="n">k1</span><span class="o">=</span><span class="n">o</span><span class="p">,</span><span class="n">k2</span><span class="o">=</span><span class="n">o</span><span class="p">,</span><span class="n">k3</span><span class="o">=</span><span class="n">o</span><span class="p">;}</span>
<span class="linenos" data-linenos="181 "></span>    <span class="n">modpair</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">=</span> <span class="p">(</span><span class="kt">int</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">k1</span><span class="o">=</span><span class="n">o</span><span class="p">,</span><span class="n">k2</span><span class="o">=</span><span class="n">o</span><span class="p">,</span><span class="n">k3</span><span class="o">=</span><span class="n">o</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos="182 "></span>    <span class="n">modpair</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">+=</span><span class="p">(</span><span class="kt">int</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">k1</span><span class="o">+=</span><span class="n">o</span><span class="p">,</span><span class="n">k2</span><span class="o">+=</span><span class="n">o</span><span class="p">,</span><span class="n">k3</span><span class="o">+=</span><span class="n">o</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos="183 "></span>    <span class="n">modpair</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">*=</span><span class="p">(</span><span class="kt">int</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">k1</span><span class="o">*=</span><span class="n">o</span><span class="p">,</span><span class="n">k2</span><span class="o">*=</span><span class="n">o</span><span class="p">,</span><span class="n">k3</span><span class="o">*=</span><span class="n">o</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos="184 "></span>    <span class="k">friend</span> <span class="n">modpair</span> <span class="k">operator</span> <span class="o">+</span><span class="p">(</span><span class="n">modpair</span> <span class="n">a</span><span class="p">,</span><span class="kt">int</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">a</span><span class="o">+=</span><span class="n">o</span><span class="p">;}</span>
<span class="linenos" data-linenos="185 "></span>    <span class="k">friend</span> <span class="n">modpair</span> <span class="k">operator</span> <span class="o">*</span><span class="p">(</span><span class="n">modpair</span> <span class="n">a</span><span class="p">,</span><span class="kt">int</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">a</span><span class="o">*=</span><span class="n">o</span><span class="p">;}</span>
<span class="linenos" data-linenos="186 "></span>    <span class="n">modpair</span> <span class="k">operator</span><span class="o">-</span><span class="p">(){</span><span class="k">return</span> <span class="nf">modpair</span><span class="p">(</span><span class="n">k1</span><span class="p">,</span><span class="n">k2</span><span class="p">,</span><span class="n">k3</span><span class="p">);}</span>
<span class="linenos" data-linenos="187 "></span><span class="p">};</span>
<span class="linenos" data-linenos="188 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="n">pow2</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">,</span><span class="n">modpair</span> <span class="n">m</span><span class="p">){</span><span class="c1">//不保证a[0]=1 </span>
<span class="linenos" data-linenos="189 "></span>    <span class="kt">int</span> <span class="n">k</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">modint</span><span class="o">&lt;</span><span class="n">mod</span><span class="o">&gt;</span> <span class="n">val</span><span class="p">;</span>
<span class="linenos" data-linenos="190 "></span>    <span class="k">while</span><span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="n">k</span><span class="p">]</span><span class="o">==</span><span class="mi">0</span><span class="o">&amp;&amp;</span><span class="n">k</span><span class="o">&lt;</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">())</span><span class="n">k</span><span class="o">++</span><span class="p">;</span>
<span class="linenos" data-linenos="191 "></span>    <span class="k">if</span><span class="p">(</span><span class="n">k</span><span class="o">==</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">()</span><span class="o">||</span><span class="n">k</span><span class="o">!=</span><span class="mi">0</span><span class="o">&amp;&amp;</span><span class="n">m</span><span class="p">.</span><span class="n">k3</span><span class="p">.</span><span class="n">lg</span><span class="o">&gt;</span><span class="mi">8</span><span class="o">||</span><span class="mf">1l</span><span class="n">l</span><span class="o">*</span><span class="n">m</span><span class="p">.</span><span class="n">k3</span><span class="p">.</span><span class="n">x</span><span class="o">*</span><span class="n">k</span><span class="o">&gt;=</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">()){</span>
<span class="linenos" data-linenos="192 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">();</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="k">return</span> <span class="n">a</span><span class="p">;}</span><span class="c1">//bye~</span>
<span class="linenos" data-linenos="193 "></span>    <span class="n">val</span><span class="o">=</span><span class="n">a</span><span class="p">[</span><span class="n">k</span><span class="p">];</span><span class="n">poly</span> <span class="n">b</span><span class="p">;</span><span class="n">b</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">()</span><span class="o">-</span><span class="n">k</span><span class="p">);</span>
<span class="linenos" data-linenos="194 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">b</span><span class="p">.</span><span class="n">size</span><span class="p">();</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">b</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="n">k</span><span class="p">]</span><span class="o">/</span><span class="n">val</span><span class="p">;</span>
<span class="linenos" data-linenos="195 "></span>    <span class="n">b</span><span class="o">=</span><span class="n">pow</span><span class="p">(</span><span class="n">b</span><span class="p">,</span><span class="n">m</span><span class="p">.</span><span class="n">k1</span><span class="p">);</span><span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">();</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="linenos" data-linenos="196 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">b</span><span class="p">.</span><span class="n">size</span><span class="p">()</span><span class="o">&amp;&amp;</span><span class="n">i</span><span class="o">+</span><span class="n">k</span><span class="o">*</span><span class="n">m</span><span class="p">.</span><span class="n">k1</span><span class="p">.</span><span class="n">x</span><span class="o">&lt;</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">();</span><span class="n">i</span><span class="o">++</span><span class="p">)</span>
<span class="linenos" data-linenos="197 "></span>        <span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="n">k</span><span class="o">*</span><span class="n">m</span><span class="p">.</span><span class="n">k1</span><span class="p">.</span><span class="n">x</span><span class="p">]</span><span class="o">=</span><span class="n">b</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">*</span><span class="p">(</span><span class="n">val</span><span class="o">^</span><span class="n">m</span><span class="p">.</span><span class="n">k2</span><span class="p">.</span><span class="n">x</span><span class="p">);</span>
<span class="linenos" data-linenos="198 "></span>    <span class="k">return</span> <span class="n">a</span><span class="p">;</span>
<span class="linenos" data-linenos="199 "></span><span class="p">}</span>
<span class="linenos" data-linenos="200 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="n">sin</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">){</span><span class="k">return</span> <span class="p">(</span><span class="n">exp</span><span class="p">(</span><span class="n">a</span><span class="o">*</span><span class="n">I</span><span class="p">)</span><span class="o">-</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">a</span><span class="o">*</span><span class="n">I</span><span class="p">))</span><span class="o">/</span><span class="p">(</span><span class="n">I</span><span class="o">*</span><span class="mi">2</span><span class="p">);}</span>
<span class="linenos" data-linenos="201 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="n">cos</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">){</span><span class="k">return</span> <span class="p">(</span><span class="n">exp</span><span class="p">(</span><span class="n">a</span><span class="o">*</span><span class="n">I</span><span class="p">)</span><span class="o">+</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">a</span><span class="o">*</span><span class="n">I</span><span class="p">))</span><span class="o">/</span><span class="mi">2</span><span class="p">;}</span>
<span class="linenos" data-linenos="202 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="n">asin</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">){</span>
<span class="linenos" data-linenos="203 "></span>    <span class="n">poly</span> <span class="n">G</span><span class="o">=-</span><span class="n">a</span><span class="o">*</span><span class="n">a</span><span class="p">;</span><span class="n">G</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">++</span><span class="p">;</span>
<span class="linenos" data-linenos="204 "></span>    <span class="k">return</span> <span class="nf">inter</span><span class="p">(</span><span class="n">deriv</span><span class="p">(</span><span class="n">a</span><span class="p">)</span><span class="o">*</span><span class="n">inv</span><span class="p">(</span><span class="n">sqrt</span><span class="p">(</span><span class="n">G</span><span class="p">)));</span>
<span class="linenos" data-linenos="205 "></span><span class="p">}</span>
<span class="linenos" data-linenos="206 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="n">atan</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">){</span>
<span class="linenos" data-linenos="207 "></span>    <span class="n">poly</span> <span class="n">G</span><span class="o">=</span><span class="n">a</span><span class="o">*</span><span class="n">a</span><span class="p">;</span><span class="n">G</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">++</span><span class="p">;</span>
<span class="linenos" data-linenos="208 "></span>    <span class="k">return</span> <span class="nf">inter</span><span class="p">(</span><span class="n">deriv</span><span class="p">(</span><span class="n">a</span><span class="p">)</span><span class="o">*</span><span class="n">inv</span><span class="p">(</span><span class="n">G</span><span class="p">));</span>
<span class="linenos" data-linenos="209 "></span><span class="p">}</span>
</code></pre></div>
</div></div>
<h2 id="_12">多项式黑科技</h2>
<h3 id="nttmtt">任意模数NTT(MTT)</h3>
<p>把两个多项式 <script type="math/tex">A(x),B(x)</script> 分别拆成 <script type="math/tex">A_0(x),A_1(x),B_0(x),B_1(x)</script>后，求出它们的点值表示。</p>
<p>构造两个多项式：
<script type="math/tex; mode=display">P(x)=A(x)+iB(x)</script>
<script type="math/tex; mode=display">Q(x)=A(x)+iB(x)</script>
由于<script type="math/tex">A,B</script>的虚部都为<script type="math/tex">0</script>，<script type="math/tex">P,Q</script>的每一项系数都互为共轭。对<script type="math/tex">P</script>做一遍DFT,可以<code>std::conj</code>在<script type="math/tex">O(n)</script>求出<script type="math/tex">Q</script>的点值表示。然后<script type="math/tex">A_0,A_1,B_0,B_1</script>的点值就是<sub>~有手就行</sub>~了</p>
<p>接下求<script type="math/tex">A,B</script>之间的两两乘积，乘出来对<script type="math/tex">A_0B_0,A_1B_0,A_0B_0,A_0B_1</script>做IDFT。</p>
<p>由于虚部不为0，但仍然可以借用上述的方法。构造两个多项式：</p>
<p>
<script type="math/tex; mode=display">P(x)=A_0(x)B_0(x)+iA_1(x)B_0(x)</script>
<script type="math/tex; mode=display">Q(X)=A_0(x)B_1(x)+iA_1(x)B_1(x)</script>
</p>
<p>分别做IDFT，由于 <script type="math/tex">A_0B_0,A_0B_1,A_1B_0,A_1B_1</script>这四个多项式卷起来后的系数表示中虚部一定为<script type="math/tex">0</script>，那么<script type="math/tex">P</script>的实部与虚部就分别为<script type="math/tex">A_0(x)B_0(x)</script>和<script type="math/tex">A_1(x)B_0(x)</script>，而<script type="math/tex">Q</script>就位<script type="math/tex">A_0(x)B_1(x)</script>与<script type="math/tex">A_1(x)B_1(x)</script>
</p>
<p>给出部分代码：
<div class="highlight"><pre><span></span><code><span class="linenos" data-linenos="  1 "></span><span class="k">typedef</span> <span class="n">std</span><span class="o">::</span><span class="n">complex</span><span class="o">&lt;</span><span class="kt">double</span><span class="o">&gt;</span><span class="n">complex</span><span class="p">;</span>
<span class="linenos" data-linenos="  2 "></span><span class="k">const</span> <span class="kt">int</span> <span class="n">N</span><span class="o">=</span><span class="mf">4e6</span><span class="o">+</span><span class="mi">10</span><span class="p">;</span><span class="k">const</span> <span class="kt">double</span> <span class="n">PI</span><span class="o">=</span><span class="n">acos</span><span class="p">(</span><span class="mi">-1</span><span class="p">);</span><span class="k">const</span> <span class="n">complex</span> <span class="n">I</span><span class="o">=</span><span class="n">complex</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">);</span>
<span class="linenos" data-linenos="  3 "></span><span class="kt">int</span> <span class="n">rev</span><span class="p">[</span><span class="n">N</span><span class="p">];</span><span class="n">complex</span> <span class="n">Wn</span><span class="p">[</span><span class="n">N</span><span class="p">];</span><span class="kt">int</span> <span class="n">M</span><span class="p">,</span><span class="n">mod</span><span class="p">;</span>
<span class="linenos" data-linenos="  4 "></span><span class="k">struct</span> <span class="nc">modint</span><span class="p">{</span>
<span class="linenos" data-linenos="  5 "></span>    <span class="kt">int</span> <span class="n">x</span><span class="p">;</span>
<span class="linenos" data-linenos="  6 "></span>    <span class="n">modint</span><span class="p">(</span><span class="kt">int</span> <span class="n">o</span><span class="o">=</span><span class="mi">0</span><span class="p">){</span><span class="n">x</span><span class="o">=</span><span class="n">o</span><span class="p">;}</span>
<span class="linenos" data-linenos="  7 "></span>    <span class="n">modint</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">=</span> <span class="p">(</span><span class="kt">int</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">x</span><span class="o">=</span><span class="n">o</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos="  8 "></span>    <span class="n">modint</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">+=</span><span class="p">(</span><span class="n">modint</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">x</span><span class="o">=</span><span class="n">x</span><span class="o">+</span><span class="n">o</span><span class="p">.</span><span class="n">x</span><span class="o">&gt;=</span><span class="n">mod</span><span class="o">?</span><span class="n">x</span><span class="o">+</span><span class="n">o</span><span class="p">.</span><span class="n">x</span><span class="o">-</span><span class="nl">mod</span><span class="p">:</span><span class="n">x</span><span class="o">+</span><span class="n">o</span><span class="p">.</span><span class="n">x</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos="  9 "></span>    <span class="n">modint</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">-=</span><span class="p">(</span><span class="n">modint</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">x</span><span class="o">=</span><span class="n">x</span><span class="o">-</span><span class="n">o</span><span class="p">.</span><span class="n">x</span><span class="o">&lt;</span><span class="mi">0</span><span class="o">?</span><span class="n">x</span><span class="o">-</span><span class="n">o</span><span class="p">.</span><span class="n">x</span><span class="o">+</span><span class="nl">mod</span><span class="p">:</span><span class="n">x</span><span class="o">-</span><span class="n">o</span><span class="p">.</span><span class="n">x</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 10 "></span>    <span class="n">modint</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">*=</span><span class="p">(</span><span class="n">modint</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">x</span><span class="o">=</span><span class="mf">1l</span><span class="n">l</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">o</span><span class="p">.</span><span class="n">x</span><span class="o">%</span><span class="n">mod</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 11 "></span>    <span class="n">modint</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">^=</span><span class="p">(</span><span class="kt">int</span> <span class="n">b</span><span class="p">){</span>
<span class="linenos" data-linenos=" 12 "></span>        <span class="n">modint</span> <span class="n">a</span><span class="o">=*</span><span class="k">this</span><span class="p">,</span><span class="n">c</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span>
<span class="linenos" data-linenos=" 13 "></span>        <span class="k">for</span><span class="p">(;</span><span class="n">b</span><span class="p">;</span><span class="n">b</span><span class="o">&gt;&gt;=</span><span class="mi">1</span><span class="p">,</span><span class="n">a</span><span class="o">*=</span><span class="n">a</span><span class="p">)</span><span class="k">if</span><span class="p">(</span><span class="n">b</span><span class="o">&amp;</span><span class="mi">1</span><span class="p">)</span><span class="n">c</span><span class="o">*=</span><span class="n">a</span><span class="p">;</span>
<span class="linenos" data-linenos=" 14 "></span>        <span class="k">return</span> <span class="n">x</span><span class="o">=</span><span class="n">c</span><span class="p">.</span><span class="n">x</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;</span>
<span class="linenos" data-linenos=" 15 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos=" 16 "></span>    <span class="n">modint</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">/=</span><span class="p">(</span><span class="n">modint</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="o">*</span><span class="k">this</span> <span class="o">*=</span><span class="n">o</span><span class="o">^=</span><span class="n">mod</span><span class="mi">-2</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 17 "></span>    <span class="n">modint</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">+=</span><span class="p">(</span><span class="kt">int</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">x</span><span class="o">=</span><span class="n">x</span><span class="o">+</span><span class="n">o</span><span class="o">&gt;=</span><span class="n">mod</span><span class="o">?</span><span class="n">x</span><span class="o">+</span><span class="n">o</span><span class="o">-</span><span class="nl">mod</span><span class="p">:</span><span class="n">x</span><span class="o">+</span><span class="n">o</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 18 "></span>    <span class="n">modint</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">-=</span><span class="p">(</span><span class="kt">int</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">x</span><span class="o">=</span><span class="n">x</span><span class="o">-</span><span class="n">o</span><span class="o">&lt;</span><span class="mi">0</span><span class="o">?</span><span class="n">x</span><span class="o">-</span><span class="n">o</span><span class="o">+</span><span class="nl">mod</span><span class="p">:</span><span class="n">x</span><span class="o">-</span><span class="n">o</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 19 "></span>    <span class="n">modint</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">*=</span><span class="p">(</span><span class="kt">int</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="n">x</span><span class="o">=</span><span class="mf">1l</span><span class="n">l</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">o</span><span class="o">%</span><span class="n">mod</span><span class="p">,</span><span class="o">*</span><span class="k">this</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 20 "></span>    <span class="n">modint</span> <span class="o">&amp;</span><span class="k">operator</span> <span class="o">/=</span><span class="p">(</span><span class="kt">int</span> <span class="n">o</span><span class="p">){</span><span class="k">return</span> <span class="o">*</span><span class="k">this</span> <span class="o">*=</span> <span class="p">((</span><span class="n">modint</span><span class="p">(</span><span class="n">o</span><span class="p">))</span><span class="o">^=</span><span class="n">mod</span><span class="mi">-2</span><span class="p">);}</span>
<span class="linenos" data-linenos=" 21 "></span>    <span class="k">template</span><span class="o">&lt;</span><span class="k">class</span> <span class="nc">I</span><span class="o">&gt;</span><span class="k">friend</span> <span class="n">modint</span> <span class="k">operator</span> <span class="o">+</span><span class="p">(</span><span class="n">modint</span> <span class="n">a</span><span class="p">,</span><span class="n">I</span> <span class="n">b</span><span class="p">){</span><span class="k">return</span> <span class="n">a</span><span class="o">+=</span><span class="n">b</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 22 "></span>    <span class="k">template</span><span class="o">&lt;</span><span class="k">class</span> <span class="nc">I</span><span class="o">&gt;</span><span class="k">friend</span> <span class="n">modint</span> <span class="k">operator</span> <span class="o">-</span><span class="p">(</span><span class="n">modint</span> <span class="n">a</span><span class="p">,</span><span class="n">I</span> <span class="n">b</span><span class="p">){</span><span class="k">return</span> <span class="n">a</span><span class="o">-=</span><span class="n">b</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 23 "></span>    <span class="k">template</span><span class="o">&lt;</span><span class="k">class</span> <span class="nc">I</span><span class="o">&gt;</span><span class="k">friend</span> <span class="n">modint</span> <span class="k">operator</span> <span class="o">*</span><span class="p">(</span><span class="n">modint</span> <span class="n">a</span><span class="p">,</span><span class="n">I</span> <span class="n">b</span><span class="p">){</span><span class="k">return</span> <span class="n">a</span><span class="o">*=</span><span class="n">b</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 24 "></span>    <span class="k">template</span><span class="o">&lt;</span><span class="k">class</span> <span class="nc">I</span><span class="o">&gt;</span><span class="k">friend</span> <span class="n">modint</span> <span class="k">operator</span> <span class="o">/</span><span class="p">(</span><span class="n">modint</span> <span class="n">a</span><span class="p">,</span><span class="n">I</span> <span class="n">b</span><span class="p">){</span><span class="k">return</span> <span class="n">a</span><span class="o">/=</span><span class="n">b</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 25 "></span>    <span class="k">friend</span> <span class="n">modint</span> <span class="k">operator</span> <span class="o">^</span><span class="p">(</span><span class="n">modint</span> <span class="n">a</span><span class="p">,</span><span class="kt">int</span> <span class="n">b</span><span class="p">){</span><span class="k">return</span> <span class="n">a</span><span class="o">^=</span><span class="n">b</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 26 "></span>    <span class="k">friend</span> <span class="kt">bool</span> <span class="k">operator</span> <span class="o">==</span><span class="p">(</span><span class="n">modint</span> <span class="n">a</span><span class="p">,</span><span class="kt">int</span> <span class="n">b</span><span class="p">){</span><span class="k">return</span> <span class="n">a</span><span class="p">.</span><span class="n">x</span><span class="o">==</span><span class="n">b</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 27 "></span>    <span class="k">friend</span> <span class="kt">bool</span> <span class="k">operator</span> <span class="o">!=</span><span class="p">(</span><span class="n">modint</span> <span class="n">a</span><span class="p">,</span><span class="kt">int</span> <span class="n">b</span><span class="p">){</span><span class="k">return</span> <span class="n">a</span><span class="p">.</span><span class="n">x</span><span class="o">!=</span><span class="n">b</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 28 "></span>    <span class="kt">bool</span> <span class="k">operator</span> <span class="o">!</span> <span class="p">()</span> <span class="p">{</span><span class="k">return</span> <span class="o">!</span><span class="n">x</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 29 "></span>    <span class="n">modint</span> <span class="k">operator</span> <span class="o">-</span> <span class="p">()</span> <span class="p">{</span><span class="k">return</span> <span class="n">x</span><span class="o">?</span><span class="n">mod</span><span class="o">-</span><span class="nl">x</span><span class="p">:</span><span class="mi">0</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 30 "></span>    <span class="n">modint</span> <span class="o">&amp;</span><span class="k">operator</span><span class="o">++</span><span class="p">(</span><span class="kt">int</span><span class="p">){</span><span class="k">return</span> <span class="o">*</span><span class="k">this</span><span class="o">+=</span><span class="mi">1</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 31 "></span><span class="p">};</span>
<span class="linenos" data-linenos=" 32 "></span><span class="kr">inline</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">num</span><span class="p">(</span><span class="n">complex</span> <span class="n">x</span><span class="p">){</span><span class="kt">double</span> <span class="n">d</span><span class="o">=</span><span class="n">x</span><span class="p">.</span><span class="n">real</span><span class="p">();</span><span class="k">return</span> <span class="n">d</span><span class="o">&lt;</span><span class="mi">0</span><span class="o">?</span><span class="p">(</span><span class="kt">long</span> <span class="kt">long</span><span class="p">)(</span><span class="n">d</span><span class="mf">-0.5</span><span class="p">)</span><span class="o">%</span><span class="nl">mod</span><span class="p">:(</span><span class="kt">long</span> <span class="kt">long</span><span class="p">)(</span><span class="n">d</span><span class="o">+</span><span class="mf">0.5</span><span class="p">)</span><span class="o">%</span><span class="n">mod</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 33 "></span><span class="k">struct</span> <span class="nc">poly</span><span class="p">{</span>
<span class="linenos" data-linenos=" 34 "></span>    <span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">complex</span><span class="o">&gt;</span><span class="n">a0</span><span class="p">,</span><span class="n">a1</span><span class="p">;</span>
<span class="linenos" data-linenos=" 35 "></span>    <span class="kt">int</span> <span class="nf">size</span><span class="p">(){</span><span class="k">return</span> <span class="n">a0</span><span class="p">.</span><span class="n">size</span><span class="p">();}</span>
<span class="linenos" data-linenos=" 36 "></span>    <span class="kt">void</span> <span class="nf">resize</span><span class="p">(</span><span class="kt">int</span> <span class="n">n</span><span class="p">){</span><span class="n">a0</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span><span class="n">a1</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);}</span>
<span class="linenos" data-linenos=" 37 "></span>    <span class="kt">void</span> <span class="nf">set</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span><span class="n">modint</span> <span class="n">y</span><span class="p">){</span>
<span class="linenos" data-linenos=" 38 "></span>        <span class="n">a0</span><span class="p">[</span><span class="n">x</span><span class="p">]</span><span class="o">=</span><span class="n">y</span><span class="p">.</span><span class="n">x</span><span class="o">/</span><span class="n">M</span><span class="p">;</span>
<span class="linenos" data-linenos=" 39 "></span>        <span class="n">a1</span><span class="p">[</span><span class="n">x</span><span class="p">]</span><span class="o">=</span><span class="n">y</span><span class="p">.</span><span class="n">x</span><span class="o">%</span><span class="n">M</span><span class="p">;</span>
<span class="linenos" data-linenos=" 40 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos=" 41 "></span>    <span class="kt">long</span> <span class="kt">long</span> <span class="n">get</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">){</span><span class="k">return</span> <span class="p">(</span><span class="n">M</span><span class="o">*</span><span class="n">M</span><span class="o">*</span><span class="n">num</span><span class="p">(</span><span class="n">a0</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">real</span><span class="p">())</span><span class="o">%</span><span class="n">mod</span> <span class="o">+</span>
<span class="linenos" data-linenos=" 42 "></span>                <span class="n">M</span><span class="o">*</span><span class="p">(</span><span class="n">num</span><span class="p">(</span><span class="n">a0</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">imag</span><span class="p">())</span><span class="o">+</span><span class="n">num</span><span class="p">(</span><span class="n">a1</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">real</span><span class="p">()))</span><span class="o">%</span><span class="n">mod</span><span class="o">+</span><span class="n">num</span><span class="p">(</span><span class="n">a1</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">imag</span><span class="p">()))</span><span class="o">%</span><span class="n">mod</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 43 "></span>    <span class="n">modint</span> <span class="n">val</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">){</span><span class="k">return</span> <span class="p">(</span><span class="kt">long</span> <span class="kt">long</span><span class="p">)(</span><span class="n">M</span><span class="o">*</span><span class="n">a0</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">real</span><span class="p">()</span><span class="o">+</span><span class="n">a1</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">real</span><span class="p">()</span><span class="o">+</span><span class="n">mod</span><span class="p">)</span><span class="o">%</span><span class="n">mod</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 44 "></span><span class="p">};</span>
<span class="linenos" data-linenos=" 45 "></span><span class="n">poly</span> <span class="k">operator</span><span class="o">+</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">,</span><span class="n">poly</span> <span class="n">b</span><span class="p">){</span>
<span class="linenos" data-linenos=" 46 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="n">std</span><span class="o">::</span><span class="n">max</span><span class="p">(</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">(),</span><span class="n">b</span><span class="p">.</span><span class="n">size</span><span class="p">());</span><span class="n">a</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">),</span><span class="n">b</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="linenos" data-linenos=" 47 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">a</span><span class="p">.</span><span class="n">set</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">a</span><span class="p">.</span><span class="n">val</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="o">+</span><span class="n">b</span><span class="p">.</span><span class="n">val</span><span class="p">(</span><span class="n">i</span><span class="p">));</span><span class="k">return</span> <span class="n">a</span><span class="p">;</span>
<span class="linenos" data-linenos=" 48 "></span><span class="p">}</span>
<span class="linenos" data-linenos=" 49 "></span><span class="n">poly</span> <span class="k">operator</span><span class="o">-</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">,</span><span class="n">poly</span> <span class="n">b</span><span class="p">){</span>
<span class="linenos" data-linenos=" 50 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="n">std</span><span class="o">::</span><span class="n">max</span><span class="p">(</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">(),</span><span class="n">b</span><span class="p">.</span><span class="n">size</span><span class="p">());</span><span class="n">a</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">),</span><span class="n">b</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="linenos" data-linenos=" 51 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">a</span><span class="p">.</span><span class="n">set</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">a</span><span class="p">.</span><span class="n">val</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="o">-</span><span class="n">b</span><span class="p">.</span><span class="n">val</span><span class="p">(</span><span class="n">i</span><span class="p">));</span><span class="k">return</span> <span class="n">a</span><span class="p">;</span>
<span class="linenos" data-linenos=" 52 "></span><span class="p">}</span>
<span class="linenos" data-linenos=" 53 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="n">one</span><span class="p">(){</span><span class="n">poly</span> <span class="n">a</span><span class="p">;</span><span class="n">a</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span><span class="n">a</span><span class="p">.</span><span class="n">set</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">);</span><span class="k">return</span> <span class="n">a</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 54 "></span><span class="kr">inline</span> <span class="kt">int</span> <span class="n">ext</span><span class="p">(</span><span class="kt">int</span> <span class="n">n</span><span class="p">){</span><span class="kt">int</span> <span class="n">k</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="k">while</span><span class="p">((</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="n">k</span><span class="p">)</span><span class="o">&lt;</span><span class="n">n</span><span class="p">)</span><span class="n">k</span><span class="o">++</span><span class="p">;</span><span class="k">return</span> <span class="n">k</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 55 "></span><span class="kr">inline</span> <span class="kt">void</span> <span class="n">init</span><span class="p">(</span><span class="kt">int</span> <span class="n">k</span><span class="p">){</span>
<span class="linenos" data-linenos=" 56 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="n">k</span><span class="p">;</span>
<span class="linenos" data-linenos=" 57 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span>
<span class="linenos" data-linenos=" 58 "></span>        <span class="n">rev</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="p">(</span><span class="n">rev</span><span class="p">[</span><span class="n">i</span><span class="o">&gt;&gt;</span><span class="mi">1</span><span class="p">]</span><span class="o">&gt;&gt;</span><span class="mi">1</span><span class="p">)</span><span class="o">|</span><span class="p">((</span><span class="n">i</span><span class="o">&amp;</span><span class="mi">1</span><span class="p">)</span><span class="o">&lt;&lt;</span><span class="p">(</span><span class="n">k</span><span class="mi">-1</span><span class="p">));</span>
<span class="linenos" data-linenos=" 59 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span>
<span class="linenos" data-linenos=" 60 "></span>        <span class="n">Wn</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="p">{</span><span class="n">cos</span><span class="p">(</span><span class="n">PI</span><span class="o">/</span><span class="n">n</span><span class="o">*</span><span class="n">i</span><span class="p">),</span><span class="n">sin</span><span class="p">(</span><span class="n">PI</span><span class="o">/</span><span class="n">n</span><span class="o">*</span><span class="n">i</span><span class="p">)};</span>
<span class="linenos" data-linenos=" 61 "></span><span class="p">}</span>
<span class="linenos" data-linenos=" 62 "></span><span class="kt">void</span> <span class="n">FFT</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">complex</span><span class="o">&gt;&amp;</span><span class="n">A</span><span class="p">,</span><span class="kt">int</span> <span class="n">n</span><span class="p">,</span><span class="kt">int</span> <span class="n">t</span><span class="p">){</span>
<span class="linenos" data-linenos=" 63 "></span>    <span class="k">if</span><span class="p">(</span><span class="n">t</span><span class="o">&lt;</span><span class="mi">0</span><span class="p">)</span><span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="k">if</span><span class="p">(</span><span class="n">i</span><span class="o">&lt;</span><span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="n">i</span><span class="p">))</span><span class="n">std</span><span class="o">::</span><span class="n">swap</span><span class="p">(</span><span class="n">A</span><span class="p">[</span><span class="n">i</span><span class="p">],</span><span class="n">A</span><span class="p">[</span><span class="n">n</span><span class="o">-</span><span class="n">i</span><span class="p">]);</span>
<span class="linenos" data-linenos=" 64 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span>
<span class="linenos" data-linenos=" 65 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">i</span><span class="o">&lt;</span><span class="n">rev</span><span class="p">[</span><span class="n">i</span><span class="p">])</span><span class="n">std</span><span class="o">::</span><span class="n">swap</span><span class="p">(</span><span class="n">A</span><span class="p">[</span><span class="n">i</span><span class="p">],</span><span class="n">A</span><span class="p">[</span><span class="n">rev</span><span class="p">[</span><span class="n">i</span><span class="p">]]);</span>
<span class="linenos" data-linenos=" 66 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">m</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span><span class="n">m</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">m</span><span class="o">&lt;&lt;=</span><span class="mi">1</span><span class="p">)</span>
<span class="linenos" data-linenos=" 67 "></span>        <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">+=</span><span class="n">m</span><span class="o">&lt;&lt;</span><span class="mi">1</span><span class="p">)</span>
<span class="linenos" data-linenos=" 68 "></span>            <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">k</span><span class="o">=</span><span class="n">i</span><span class="p">;</span><span class="n">k</span><span class="o">&lt;</span><span class="n">i</span><span class="o">+</span><span class="n">m</span><span class="p">;</span><span class="n">k</span><span class="o">++</span><span class="p">){</span>
<span class="linenos" data-linenos=" 69 "></span>                <span class="n">complex</span> <span class="n">W</span><span class="o">=</span><span class="n">Wn</span><span class="p">[</span><span class="mf">1l</span><span class="n">l</span><span class="o">*</span><span class="p">(</span><span class="n">k</span><span class="o">-</span><span class="n">i</span><span class="p">)</span><span class="o">*</span><span class="n">n</span><span class="o">/</span><span class="n">m</span><span class="p">];</span>
<span class="linenos" data-linenos=" 70 "></span>                <span class="n">complex</span> <span class="n">a0</span><span class="o">=</span><span class="n">A</span><span class="p">[</span><span class="n">k</span><span class="p">],</span><span class="n">a1</span><span class="o">=</span><span class="n">A</span><span class="p">[</span><span class="n">k</span><span class="o">+</span><span class="n">m</span><span class="p">]</span><span class="o">*</span><span class="n">W</span><span class="p">;</span>
<span class="linenos" data-linenos=" 71 "></span>                <span class="n">A</span><span class="p">[</span><span class="n">k</span><span class="p">]</span><span class="o">=</span><span class="n">a0</span><span class="o">+</span><span class="n">a1</span><span class="p">;</span><span class="n">A</span><span class="p">[</span><span class="n">k</span><span class="o">+</span><span class="n">m</span><span class="p">]</span><span class="o">=</span><span class="n">a0</span><span class="o">-</span><span class="n">a1</span><span class="p">;</span> 
<span class="linenos" data-linenos=" 72 "></span>            <span class="p">}</span>
<span class="linenos" data-linenos=" 73 "></span>    <span class="k">if</span><span class="p">(</span><span class="n">t</span><span class="o">&lt;</span><span class="mi">0</span><span class="p">)</span><span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">A</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">/=</span><span class="n">n</span><span class="p">;</span>
<span class="linenos" data-linenos=" 74 "></span><span class="p">}</span>
<span class="linenos" data-linenos=" 75 "></span><span class="kt">void</span> <span class="n">MTT</span><span class="p">(</span><span class="n">poly</span> <span class="o">&amp;</span><span class="n">A</span><span class="p">,</span><span class="kt">int</span> <span class="n">n</span><span class="p">,</span><span class="kt">int</span> <span class="n">t</span><span class="p">){</span>
<span class="linenos" data-linenos=" 76 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">A</span><span class="p">.</span><span class="n">a0</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="n">A</span><span class="p">.</span><span class="n">a0</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">+</span><span class="n">I</span><span class="o">*</span><span class="n">A</span><span class="p">.</span><span class="n">a1</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
<span class="linenos" data-linenos=" 77 "></span>    <span class="n">FFT</span><span class="p">(</span><span class="n">A</span><span class="p">.</span><span class="n">a0</span><span class="p">,</span><span class="n">n</span><span class="p">,</span><span class="n">t</span><span class="p">);</span>
<span class="linenos" data-linenos=" 78 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">A</span><span class="p">.</span><span class="n">a1</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="n">std</span><span class="o">::</span><span class="n">conj</span><span class="p">(</span><span class="n">A</span><span class="p">.</span><span class="n">a0</span><span class="p">[</span><span class="n">i</span><span class="o">?</span><span class="n">n</span><span class="o">-</span><span class="nl">i</span><span class="p">:</span><span class="mi">0</span><span class="p">]);</span>
<span class="linenos" data-linenos=" 79 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">){</span>
<span class="linenos" data-linenos=" 80 "></span>        <span class="n">complex</span> <span class="n">p</span><span class="o">=</span><span class="n">A</span><span class="p">.</span><span class="n">a0</span><span class="p">[</span><span class="n">i</span><span class="p">],</span><span class="n">q</span><span class="o">=</span><span class="n">A</span><span class="p">.</span><span class="n">a1</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
<span class="linenos" data-linenos=" 81 "></span>        <span class="n">A</span><span class="p">.</span><span class="n">a0</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="p">(</span><span class="n">p</span><span class="o">+</span><span class="n">q</span><span class="p">)</span><span class="o">*</span><span class="mf">0.5</span><span class="p">;</span><span class="n">A</span><span class="p">.</span><span class="n">a1</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="p">(</span><span class="n">q</span><span class="o">-</span><span class="n">p</span><span class="p">)</span><span class="o">*</span><span class="mf">0.5</span><span class="o">*</span><span class="n">I</span><span class="p">;</span>
<span class="linenos" data-linenos=" 82 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos=" 83 "></span><span class="p">}</span>
<span class="linenos" data-linenos=" 84 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="k">operator</span><span class="o">*</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">,</span><span class="n">poly</span> <span class="n">b</span><span class="p">){</span>
<span class="linenos" data-linenos=" 85 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">()</span><span class="o">+</span><span class="n">b</span><span class="p">.</span><span class="n">size</span><span class="p">()</span><span class="mi">-1</span><span class="p">,</span><span class="n">k</span><span class="o">=</span><span class="n">ext</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="linenos" data-linenos=" 86 "></span>    <span class="n">a</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="n">k</span><span class="p">),</span><span class="n">b</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="n">k</span><span class="p">),</span><span class="n">init</span><span class="p">(</span><span class="n">k</span><span class="p">);</span>
<span class="linenos" data-linenos=" 87 "></span>    <span class="n">MTT</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="n">k</span><span class="p">,</span><span class="mi">1</span><span class="p">);</span><span class="n">MTT</span><span class="p">(</span><span class="n">b</span><span class="p">,</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="n">k</span><span class="p">,</span><span class="mi">1</span><span class="p">);</span>
<span class="linenos" data-linenos=" 88 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="p">(</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="n">k</span><span class="p">);</span><span class="n">i</span><span class="o">++</span><span class="p">){</span>
<span class="linenos" data-linenos=" 89 "></span>        <span class="n">complex</span> <span class="n">p</span><span class="o">=</span><span class="n">a</span><span class="p">.</span><span class="n">a0</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">*</span><span class="n">b</span><span class="p">.</span><span class="n">a0</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">+</span><span class="n">I</span><span class="o">*</span><span class="n">a</span><span class="p">.</span><span class="n">a1</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">*</span><span class="n">b</span><span class="p">.</span><span class="n">a0</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
<span class="linenos" data-linenos=" 90 "></span>        <span class="n">complex</span> <span class="n">q</span><span class="o">=</span><span class="n">a</span><span class="p">.</span><span class="n">a0</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">*</span><span class="n">b</span><span class="p">.</span><span class="n">a1</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">+</span><span class="n">I</span><span class="o">*</span><span class="n">a</span><span class="p">.</span><span class="n">a1</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">*</span><span class="n">b</span><span class="p">.</span><span class="n">a1</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
<span class="linenos" data-linenos=" 91 "></span>        <span class="n">a</span><span class="p">.</span><span class="n">a0</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="n">p</span><span class="p">,</span><span class="n">a</span><span class="p">.</span><span class="n">a1</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="n">q</span><span class="p">;</span>
<span class="linenos" data-linenos=" 92 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos=" 93 "></span>    <span class="n">FFT</span><span class="p">(</span><span class="n">a</span><span class="p">.</span><span class="n">a0</span><span class="p">,</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="n">k</span><span class="p">,</span><span class="mi">-1</span><span class="p">);</span><span class="n">FFT</span><span class="p">(</span><span class="n">a</span><span class="p">.</span><span class="n">a1</span><span class="p">,</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="n">k</span><span class="p">,</span><span class="mi">-1</span><span class="p">);</span><span class="n">a</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="linenos" data-linenos=" 94 "></span>    <span class="kt">long</span> <span class="kt">long</span> <span class="n">tmp</span><span class="p">;</span><span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span>
<span class="linenos" data-linenos=" 95 "></span>        <span class="n">tmp</span><span class="o">=</span><span class="n">a</span><span class="p">.</span><span class="n">get</span><span class="p">(</span><span class="n">i</span><span class="p">),</span><span class="n">a</span><span class="p">.</span><span class="n">set</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">tmp</span><span class="p">);</span>
<span class="linenos" data-linenos=" 96 "></span>    <span class="k">return</span> <span class="n">a</span><span class="p">;</span>
<span class="linenos" data-linenos=" 97 "></span><span class="p">}</span>
<span class="linenos" data-linenos=" 98 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="n">deriv</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">){</span><span class="c1">//求导 </span>
<span class="linenos" data-linenos=" 99 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">()</span><span class="mi">-1</span><span class="p">;</span>
<span class="linenos" data-linenos="100 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">a</span><span class="p">.</span><span class="n">set</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">a</span><span class="p">.</span><span class="n">val</span><span class="p">(</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">));</span>
<span class="linenos" data-linenos="101 "></span>    <span class="n">a</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span><span class="k">return</span> <span class="n">a</span><span class="p">;</span>
<span class="linenos" data-linenos="102 "></span><span class="p">}</span>
<span class="linenos" data-linenos="103 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="n">inter</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">){</span><span class="c1">//求原 </span>
<span class="linenos" data-linenos="104 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">()</span><span class="o">+</span><span class="mi">1</span><span class="p">;</span><span class="n">a</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="linenos" data-linenos="105 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">&gt;=</span><span class="mi">1</span><span class="p">;</span><span class="n">i</span><span class="o">--</span><span class="p">)</span><span class="n">a</span><span class="p">.</span><span class="n">set</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">a</span><span class="p">.</span><span class="n">val</span><span class="p">(</span><span class="n">i</span><span class="mi">-1</span><span class="p">)</span><span class="o">/</span><span class="n">i</span><span class="p">);</span>
<span class="linenos" data-linenos="106 "></span>    <span class="n">a</span><span class="p">.</span><span class="n">set</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">);</span><span class="k">return</span> <span class="n">a</span><span class="p">;</span>
<span class="linenos" data-linenos="107 "></span><span class="p">}</span>
<span class="linenos" data-linenos="108 "></span><span class="n">poly</span> <span class="n">inv</span><span class="p">(</span><span class="n">poly</span> <span class="n">F</span><span class="p">,</span><span class="kt">int</span> <span class="n">k</span><span class="p">){</span>
<span class="linenos" data-linenos="109 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="n">k</span><span class="p">;</span><span class="n">F</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="linenos" data-linenos="110 "></span>    <span class="k">if</span><span class="p">(</span><span class="n">n</span><span class="o">==</span><span class="mi">1</span><span class="p">){</span><span class="n">F</span><span class="p">.</span><span class="n">set</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="n">modint</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="n">F</span><span class="p">.</span><span class="n">val</span><span class="p">(</span><span class="mi">0</span><span class="p">));</span><span class="k">return</span> <span class="n">F</span><span class="p">;}</span>
<span class="linenos" data-linenos="111 "></span>    <span class="n">poly</span> <span class="n">G</span><span class="p">,</span><span class="n">H</span><span class="o">=</span><span class="n">inv</span><span class="p">(</span><span class="n">F</span><span class="p">,</span><span class="n">k</span><span class="mi">-1</span><span class="p">);</span>
<span class="linenos" data-linenos="112 "></span>    <span class="n">G</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">),</span><span class="n">H</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="o">&lt;&lt;</span><span class="mi">1</span><span class="p">),</span><span class="n">F</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="o">&lt;&lt;</span><span class="mi">1</span><span class="p">);</span>
<span class="linenos" data-linenos="113 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">n</span><span class="o">/</span><span class="mi">2</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="n">G</span><span class="p">.</span><span class="n">set</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">H</span><span class="p">.</span><span class="n">val</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="o">*</span><span class="mi">2</span><span class="p">);</span>
<span class="linenos" data-linenos="114 "></span>    <span class="n">H</span><span class="o">=</span><span class="n">H</span><span class="o">*</span><span class="n">H</span><span class="p">;</span><span class="n">H</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span><span class="n">H</span><span class="o">=</span><span class="n">H</span><span class="o">*</span><span class="n">F</span><span class="p">;</span><span class="n">H</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="linenos" data-linenos="115 "></span>    <span class="n">G</span><span class="o">=</span><span class="n">G</span><span class="o">-</span><span class="n">H</span><span class="p">;</span><span class="k">return</span> <span class="n">G</span><span class="p">;</span>
<span class="linenos" data-linenos="116 "></span><span class="p">}</span>
<span class="linenos" data-linenos="117 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="n">inv</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">){</span>
<span class="linenos" data-linenos="118 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">();</span>
<span class="linenos" data-linenos="119 "></span>    <span class="n">a</span><span class="o">=</span><span class="n">inv</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">ext</span><span class="p">(</span><span class="n">n</span><span class="p">)),</span><span class="n">a</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span><span class="k">return</span> <span class="n">a</span><span class="p">;;</span>
<span class="linenos" data-linenos="120 "></span><span class="p">}</span>
<span class="linenos" data-linenos="121 "></span><span class="kr">inline</span> <span class="n">poly</span> <span class="n">ln</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">){</span>
<span class="linenos" data-linenos="122 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">();</span>
<span class="linenos" data-linenos="123 "></span>    <span class="n">a</span><span class="o">=</span><span class="n">inter</span><span class="p">(</span><span class="n">deriv</span><span class="p">(</span><span class="n">a</span><span class="p">)</span><span class="o">*</span><span class="n">inv</span><span class="p">(</span><span class="n">a</span><span class="p">));</span>
<span class="linenos" data-linenos="124 "></span>    <span class="n">a</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span><span class="k">return</span> <span class="n">a</span><span class="p">;</span>
<span class="linenos" data-linenos="125 "></span><span class="p">}</span>
<span class="linenos" data-linenos="126 "></span><span class="n">poly</span> <span class="n">exp</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">,</span><span class="kt">int</span> <span class="n">k</span><span class="p">){</span>
<span class="linenos" data-linenos="127 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="n">k</span><span class="p">;</span><span class="n">a</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="linenos" data-linenos="128 "></span>    <span class="k">if</span><span class="p">(</span><span class="n">n</span><span class="o">==</span><span class="mi">1</span><span class="p">)</span><span class="k">return</span> <span class="n">one</span><span class="p">();</span>
<span class="linenos" data-linenos="129 "></span>    <span class="n">poly</span> <span class="n">f0</span><span class="o">=</span><span class="n">exp</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">k</span><span class="mi">-1</span><span class="p">);</span><span class="n">f0</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="linenos" data-linenos="130 "></span>    <span class="k">return</span> <span class="n">f0</span><span class="o">*</span><span class="p">(</span><span class="n">one</span><span class="p">()</span><span class="o">+</span><span class="n">a</span><span class="o">-</span><span class="n">ln</span><span class="p">(</span><span class="n">f0</span><span class="p">));</span> 
<span class="linenos" data-linenos="131 "></span><span class="p">}</span>
<span class="linenos" data-linenos="132 "></span><span class="n">poly</span> <span class="n">exp</span><span class="p">(</span><span class="n">poly</span> <span class="n">a</span><span class="p">){</span>
<span class="linenos" data-linenos="133 "></span>    <span class="kt">int</span> <span class="n">n</span><span class="o">=</span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">();</span>
<span class="linenos" data-linenos="134 "></span>    <span class="n">a</span><span class="o">=</span><span class="n">exp</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">ext</span><span class="p">(</span><span class="n">n</span><span class="p">));</span><span class="n">a</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">n</span><span class="p">);</span><span class="k">return</span> <span class="n">a</span><span class="p">;</span>
<span class="linenos" data-linenos="135 "></span><span class="p">}</span>
</code></pre></div>
别问我<code>vector</code>太慢怎么办 <del>下次一定改</del></p>
<h3 id="_13">多项式除法</h3>
<p>太神奇了蒟蒻只能膜拜</p>
<p>设多项式<script type="math/tex">A</script>为<script type="math/tex">n</script>次多项式，考虑一种操作<script type="math/tex">R</script>，使得</p>
<p>
<script type="math/tex; mode=display">A_R(x)=x^nA(\frac 1x)</script>
</p>
<p>不难发现<script type="math/tex">A_R</script>就是对<script type="math/tex">A</script>进行系数翻转。</p>
<p>
<script type="math/tex; mode=display">F(x)=Q(x)\times G(x)+R(x)</script>
<script type="math/tex; mode=display">F(\frac 1x)=Q(\frac 1x)\times G(\frac 1x)+R(\frac 1x)</script>
<script type="math/tex; mode=display">x^nF(\frac 1x)=x^{n-m}Q(\frac 1x)\times x^mG(\frac 1x)+x^{n-m+1}\times x^{m-1}R(\frac 1x)</script>
<script type="math/tex; mode=display">F_R(x)=Q_R(x)\times G_R(x)+x^{n-m+1}\times R_R(x)</script>
<script type="math/tex; mode=display">F_R(x)\equiv Q_R(x)\times G_R(x) \pmod {x^{n-m+1}}</script>
<script type="math/tex; mode=display">Q_R(x)\equiv F_R(x)\times G_R^{-1}(x) \pmod {x^{n-m+1}}</script>
然后<script type="math/tex">R</script>就有手就行了
<script type="math/tex; mode=display">R(x)=F(x)-G(x)\times Q(x)</script>
<div class="highlight"><pre><span></span><code><span class="linenos" data-linenos="1 "></span><span class="kr">inline</span> <span class="n">pair</span><span class="o">&lt;</span><span class="n">poly</span><span class="p">,</span><span class="n">poly</span><span class="o">&gt;</span><span class="n">div</span><span class="p">(</span><span class="n">poly</span> <span class="n">rF</span><span class="p">,</span><span class="n">poly</span> <span class="n">rG</span><span class="p">){</span>
<span class="linenos" data-linenos="2 "></span>    <span class="n">poly</span> <span class="n">Q</span><span class="p">,</span><span class="n">R</span><span class="p">;</span><span class="kt">int</span> <span class="n">q</span><span class="o">=</span><span class="n">rF</span><span class="p">.</span><span class="n">size</span><span class="p">()</span><span class="o">-</span><span class="n">rG</span><span class="p">.</span><span class="n">size</span><span class="p">()</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span><span class="n">n</span><span class="o">=</span><span class="n">rF</span><span class="p">.</span><span class="n">size</span><span class="p">(),</span><span class="n">m</span><span class="o">=</span><span class="n">rG</span><span class="p">.</span><span class="n">size</span><span class="p">();</span><span class="n">poly</span> <span class="n">F</span><span class="o">=</span><span class="n">rF</span><span class="p">,</span><span class="n">G</span><span class="o">=</span><span class="n">rG</span><span class="p">;</span>
<span class="linenos" data-linenos="3 "></span>    <span class="n">rF</span><span class="p">.</span><span class="n">reverse</span><span class="p">();</span><span class="n">rG</span><span class="p">.</span><span class="n">reverse</span><span class="p">();</span><span class="n">rF</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">q</span><span class="p">);</span><span class="n">rG</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">q</span><span class="p">);</span>
<span class="linenos" data-linenos="4 "></span>    <span class="n">Q</span><span class="o">=</span><span class="n">rF</span><span class="o">*</span><span class="n">inv</span><span class="p">(</span><span class="n">rG</span><span class="p">);</span><span class="n">Q</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">q</span><span class="p">);</span><span class="n">Q</span><span class="p">.</span><span class="n">reverse</span><span class="p">();</span>
<span class="linenos" data-linenos="5 "></span>    <span class="n">R</span><span class="o">=</span><span class="n">F</span><span class="o">-</span><span class="p">(</span><span class="n">G</span><span class="o">*</span><span class="n">Q</span><span class="p">);</span><span class="n">R</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">m</span><span class="mi">-1</span><span class="p">);</span>
<span class="linenos" data-linenos="6 "></span>    <span class="k">return</span> <span class="p">{</span><span class="n">Q</span><span class="p">,</span><span class="n">R</span><span class="p">};</span>
<span class="linenos" data-linenos="7 "></span><span class="p">}</span>
</code></pre></div></p>
			</div>
			<div class="panel panel-default card" style="padding: 10px;height: 50px;font-size: 20px;">
				<a style="float: left;" href="\article\How_to_Count.html"><i class="fa fa-angle-double-left"></i></a><a style="float: right;" href="\article\netflow.html"><i class="fa fa-angle-double-right"></i></a>
			</div>
			<div class="panel panel-default card" style="padding: 10px;font-size: 20px;" id="vcomments">
				
			</div>
			<script>
				new Valine({
					el: '#vcomments',
					appId: 'PAlFUPg0pQVTFTEo8gCB4BCf-gzGzoHsz',
					appKey: 'U38ejnLUiizJ6vLyp6ql4hRq',
					path: window.location.pathname
				})
			</script>
		</div>
		<div class="col-md-3">
				<div class="panel panel-default card">
				<div class="panel-heading">
					<h3 class="panel-title">
						<i class="fa fa-info"></i>&emsp;
						<strong>文章信息</strong>
					</h3>
				</div>
                <div style="margin: 10px;">
                    <div style="margin-top: 8px;display: flex;"> 
    <span style="flex: 1 0 auto;margin-right: 6px;">标题</span>
    <span><font style="font-weight: bold">[营业日志]萌新的多项式入门</font></span>
	</div><div style="margin-top: 8px;display: flex;"> 
    <span style="flex: 1 0 auto;margin-right: 6px;">日期</span>
    <span>2020-12-26 18:34:04</span>
	</div>
                </div>
				</div>
				<div class="panel panel-default card">
				<div class="panel-heading">
					<h3 class="panel-title">
						<i class="fa fa-tag"></i>&emsp;
						<strong>标签</strong>
					</h3>
				</div>
				<div style="margin: 10px;">
					<a href="/tags/2020/" class="tag"><span  style="background-color: rgb(52, 152, 219);">2020</span></a>
				</div>
				</div>
				
			<div class="panel panel-default card">
					<div class="panel-heading">
						<h3 class="panel-title">
							<i class="fa fa-user-circle-o"></i>&emsp;
							<strong>caijicjl</strong>
						</h3>
					</div>
					<div style="width: 60%;display: inline-block;padding-top: 10px;">
						<ul class="propertyLinks" style="list-style-type: none;">
							<li>
								<img style="vertical-align:middle;position:relative;top:-2px" src="/img/rating-16x16.png">
								Rating:&nbsp;
								<span style="font-weight:bold;color: gray ">312</span>
							</li>
							<li>
								<img style="vertical-align:middle;position:relative;top:-2px" src="/img/star_blue_16.png">
								Contribution:&nbsp;
								<span style="color:gray;font-weight:bold;">0</span>
							</li>
						</ul>
						<ul class="nav-links">
							<li><a href="/settings/">Settings</a></li>
							<li><a href="/archives/">Blog</a></li>
							<li><a href="/teams">Teams</a></li>
							<li><a href="/submissions/dingdingsb">Submissions</a></li>
							<li><a href="/usertalk">Talks</a></li>
							<li><a href="/contests/with/dingdingsb">Contests</a></li>
						</ul>
					</div>
					<div style="display:inline-block;vertical-align: top;margin: 10px;text-align: center;">
						<div style="height:50px;width:50px"><img src="/img/avatar.png"/></div>
						<div><a href="https://www.luogu.com.cn/user/174304" style="font-weight:bold;color:gray">caijicjl</a></div>
					</div>
				</div>
		<div class="panel panel-default card">
				<div class="panel-heading">
					<h3 class="panel-title">
						<i class="fa fa-external-link"></i>&emsp;
						<strong>画中画</strong>
					</h3>
				</div>
				<div style="width:100%;margin:10px">
				<input type="text" id="url"/>
				<input value="创建" type="button" onclick="creat('kk')" /> 
				</div>
		</div>
		<script src="/js/hitokoto.js"></script>
							

				
				<span id="kk"></span>
				<div  id="myScrollspy" class="card" style="width: 100%;"  data-spy="affix">
					<script src="/js/make_toc.js"></script>
				</div>
		</div>
	</div>
 </div>
</body>
</html>
